in reply to [SOLVED] How do I determine with a regular expression whether a scalar is a number/whole/integer/float?

A couple of comments.

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:

my @numbers = qw(1, 1.0, 123.1, 0.1);
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);
  • Comment on Re: How do I determine with a regular expression whether a scalar is a number/whole/integer/float?
  • Select or Download Code

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

    Hello Laurent_R,

    Well I assumed that as soon it will match the if condition it will break. and this is the reason that I should modify it.

    Regarding the array, I do not want the array to contain strings I want the array to contain numbers. This is why I define it like this my @numbers = (1, 1.0, 123.1, 0.1);. I verified the syntax from the tutorial Arrays: A Tutorial/Reference.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
      Well I assumed that as soon it will match the if condition it will break. and this is the reason that I should modify it.
      I am not sure that I understand correctly what you are saying, and I am not sure that you understood what I was saying.

      The FAQ's original code had continue statements so that every condition is checked, even if a previous condition succeeded. So that the FAQ code, presented with 1.0 would report a non digit and then also report a real number. Your code does not do that.

      For your nested if ... elsif ... else statements to work properly, you need to reorder your conditions, for example: 1. check if it is a positive integer; 2. check for a negative integer; 3. check for a (positive or negative) float; 4. check for non-digits other than those we have accepted when checking for negative and floats.

      my @numbers = (1, 1.0, 123.1, 0.1);
      is certainly a good way of declaring an array of numbers, but I was only stressing that your use of qw/.../ was incorrect because of the commas.

        Hello again Laurent_R,

        You are right, I should not use if and else in the order that I had it before. I have updated my question/answer based on your answer it started to make more sense.

        Thanks again for your time and effort.

        Seeking for Perl wisdom...on the process of learning...not there...yet!