in reply to Numerical value of strings

Instead of avoiding the whitespace, just grab the number. Also, are you sure you need to read in the contents of the file, as simple iterating over the file will probably be fine e.g
open(DB, $database) or die "Error opening file: $!\n"; my $total = 0; while(<DB>) { my($val) = (split /\|/, $entry)[3] =~ /(\d+)/; $total += $val; } close DB;
That should iterate over your $database file, split on pipe characters, match for digits on the 4th column and then add that to $total.
HTH

_________
broquaint

Replies are listed 'Best First'.
Re: Re: Numerical value of strings
by flounder99 (Friar) on Aug 05, 2003 at 16:22 UTC
    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

      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