in reply to numbers in parentheses not considered numeric values??

gghelpneeded:

It's telling you that the string value "(49)" isn't numeric. Whenever you use a string as a number, perl tries to convert the string to a number using some simple rules. Usually it works just the way you want, but it can't possibly always do what you want.

Consider these examples:

$ cat x.pl #!/usr/bin/env perl use strict; use warnings; my @stuff = (49, (49), "49", "(49)", "123", "76 trombones", "123 E 31st", "123E31st street"); for my $t (@stuff) { print "t=<$t>, t+0=", $t+0, "\n\n"; } Roboticus@Waubli ~ $ perl x.pl t=<49>, t+0=49 t=<49>, t+0=49 t=<49>, t+0=49 Argument "(49)" isn't numeric in addition (+) at x.pl line 8. t=<(49)>, t+0=0 t=<123>, t+0=123 Argument "76 trombones" isn't numeric in addition (+) at x.pl line 8. t=<76 trombones>, t+0=76 Argument "123 E 31st" isn't numeric in addition (+) at x.pl line 8. t=<123 E 31st>, t+0=123 Argument "123E31st street" isn't numeric in addition (+) at x.pl line +8. t=<123E31st street>, t+0=1.23e+33

So if you really want to treat them as numbers, you'll need to come up with the appropriate rules (and code) to determine what number to convert them into. If it's as simple as stripping off parenthesis, you could do it with s/[()]//g, but be sure to think about all the special cases you may run into...

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Replies are listed 'Best First'.
Re^2: numbers in parentheses not considered numeric values??
by gghelpneeded (Novice) on Aug 07, 2015 at 22:37 UTC

    thanks for that example, further solidifying the explanation above. Both examples get the point across greatly!

    when you speak of stripping off parenthesis do you mean through the code itself or the find and replace feature on gedit?

      gghelpneeded:

      As the Anonymous Monk mentioned, in your code. Different sources of data may have different quirks, so you need to know about your data source to figure out the best way to clean up the data. You want to be careful with your fixes, though, to make sure you make the appropriate conversions.

      For example, in some accounting reports, parenthesis around a number may indicate that it's negative value rather than positive. Other systems may include commas, currency symbols or other formatting information. Yet other systems use a trailing sign on the data. Once you find out all the quirks, you shouldn't have too much trouble writing an appropriate subroutine to clean up and convert the data to the form you need.

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.

      In the code itself, see example below:

      #!/usr/bin/perl -l # http://perlmonks.org/?node_id=1137878 use strict; use warnings; $| = 1; while(<DATA>) { my @value = split; tr/()//d for @value; # THE FIX print $value[1] - $value[0]; } __DATA__ (15) 20 (3) 50