Mar 12, 2014, 6:17 AM
Post #2 of 3
That is not a good way to do the job in perl.
Re: [mk_bold] How to isolate numbers from a string
[In reply to]
A digit (or a period as in you example)
elsewhere in the string could change your answer.
You should use a regular expression to match the format
you expect your number to have.
The following will correctly match most decimal
numbers. If there is more than one in the string,
this returns the first of them.
$line = 'llalallala 3.5$ blabla.';
($number) = $line =~m/([-+]?\d+\.\d+)/;
An even beter way would be to use the module Regexp::Common.
It correcty handles special cases which you may not even think of.
It may be overkill for your application,
but is good to know that it is available.