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);
In reply to Re: How do I determine with a regular expression whether a scalar is a number/whole/integer/float?
by Laurent_R
in thread [SOLVED] How do I determine with a regular expression whether a scalar is a number/whole/integer/float?
by thanos1983
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |