Nestor Florez wrote:

> Hi there,
>
> I am trying to read a file and find the last numeric value in the line read.
> example:
> 123 45 -23 56 <-3.45
> 145 555 112  -12.0 -2.55
>
> all of the values are separated by a space and I am looking for the last
> value.
> I tried

If you know that the values are separated by a space you are better of using
split. (type perldoc -f split at the command line)

>
>
> $_ = s/[-][0-9].+$/$1/;
> $number = $1;

>
>
> The problem is that, not aleways I am getting the last value starting at the
> negative value.
> Somtime I get the second to last value.

The reason for this is that your regex matches a '-' sign (you don't need to
put this inside a '[ ]'). After this it matches a
number [0-9] once. A '.' is a regex metacharacter that matches any character
(except a newline). A .+$ matches all the
characters for there on till the line end.  Taking your first string as an
example
$_ = "123 45 -23 56 <-3.45";
This is how the matching happens. The '-' matches the minus sign  of -23. [0-9]
matches the 2 of -23. The .+$ matches everything after that, i.e. from the '3'
of -23 till the end of the line. The matched string is "-23 56 <-3.45". For the
second
 string the matched string is "-12.0 -2.55".

As I have mentioned before if you know the numbers are separated by a space
using split would be a better option.

hth,
Sudarsan

Reply via email to