in reply to Re: Numerical value of strings
in thread Numerical value of strings

You might want to change your regex to  /(-?\d+(?:\.\d*)?)/ in case there is a decimal or negative number in there and not just all positive integers.

--

flounder

Replies are listed 'Best First'.
Re: Re: Re: Numerical value of strings
by Limbic~Region (Chancellor) on Aug 05, 2003 at 19:56 UTC
    flounder99,
    While this increases the possibilities there are still some missing. If I were tackling this problem, I would do one of the following:
  • Correct whatever mechanism was causing/allowing the leading spaces "Malformed UTF-8 character"
  • Write a util for stripping the leading spaces "Malformed UTF-8 characters" and saving changes before running the secondary script
  • Delegate the number determination to a modified form of the looks_like_a_number from Scalar::Util. See copy/pasted excerpt below:
    sub looks_like_number { local $_ = shift; # checks from perlfaq4 return 1 unless defined; return 1 if (/^[+-]?\d+$/); # is a +/- integer return 1 if (/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/); # +a C float return 1 if ($] >= 5.008 and /^(Inf(inity)?|NaN)$/i) or ($] >= 5.006 +001 and /^Inf$/i); 0; }
    In all honesty, I would do steps 1 and 2 once and incorporate 3 as part of my regular script to ensure my data didn't get polluted with erroneous entries.

    Cheers - L~R