in reply to [SOLVED] How do I determine with a regular expression whether a scalar is a number/whole/integer/float?
The code you quote from the FAQ and the code you wrote for testing purposes are not equivalent at all. The FAQ's code will try each regex in turn, whereas your code will skip all the subsequent tests as soon as one of them is successful. So that if you test for example with -1 or 1.3, the first regex (has non digits) will match and no other test will be carried out (because they are all in an else branch of the first condition). In brief, your algorithm is wrong.
Then you also have an error in your update:
is wrong because you should not put commas in the qw// operator (see http://perldoc.perl.org/perlop.html#Quote-Like-Operators). With the above syntax, the first elements of @numbers are "1,", "1.0,", etc., so that all your elements except the last have at least one non digit, the trailing comma. You should write:my @numbers = qw(1, 1.0, 123.1, 0.1);
my @numbers = qw(1 1.0 123.1 0.1);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How do I determine with a regular expression whether a scalar is a number/whole/integer/float?
by thanos1983 (Parson) on Jul 12, 2015 at 11:09 UTC | |
by Laurent_R (Canon) on Jul 12, 2015 at 12:08 UTC | |
by thanos1983 (Parson) on Jul 12, 2015 at 13:49 UTC |