jlongino has asked for the wisdom of the Perl Monks concerning the following question:
All the example data is acceptable. I don't need a one-regex-fits-all solution--readability is more important. Rounding is unnecessary. Blanks outside of the numeric component are allowable but should be eliminated from the results. I'm not sure how to correct *problem* expression to handle the last test case (and it may allow undesirable things I'm not aware of). Here is the output from the above code:use strict; while (<DATA>) { chomp; my $input = $_; tr/$, +//d; if (/^[-]?\d+$/) { print "Acceptable: '$input' -> $_\n"; } elsif (/^[-]?\d+\.[\d]+$/) { ## problem print "Acceptable: '$input' -> ", int $_, "\n"; } else { print "Not acceptable: '$input' -> $_\n"; } } __DATA__ +30 -400 +20,0 -300.02 $ -50.1 -$ 500 - $ 60 + $ 30.
Thanks in advance for any help!Acceptable: '-400' -> -400 Acceptable: '+20,0' -> 200 Acceptable: '-300.02' -> -300 Acceptable: '$ -50.1' -> -50 Acceptable: '-$ 500' -> -500 Acceptable: '- $ 60' -> -60 Not acceptable: ' + $ 30.' -> 30.
--Jim
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Validating specific numeric input
by VSarkiss (Monsignor) on May 02, 2002 at 18:57 UTC | |
by jlongino (Parson) on May 02, 2002 at 19:08 UTC | |
|
Re: Validating specific numeric input
by tadman (Prior) on May 02, 2002 at 18:50 UTC | |
by jlongino (Parson) on May 02, 2002 at 18:57 UTC | |
|
Re: Validating specific numeric input
by Fletch (Bishop) on May 02, 2002 at 19:43 UTC | |
by jlongino (Parson) on May 02, 2002 at 20:12 UTC |