Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How do I check if an array element is a numeric value?
if ($array[0] ....)
Thanks...

Edited: ~Thu Aug 22 15:46:04 2002 (GMT) by footpad: Retitled node (and existing replies) to be more descriptive (and easier to search for), per Consideration.

Replies are listed 'Best First'.
Re: Is Array Element Numeric? (was I wonder...)
by LTjake (Prior) on Aug 22, 2002 at 11:39 UTC
    Use a regex.
    if ($array[0] =~ /^\d+$/) { # do something }
    ^ signifies the start of the string
    \d means a digit
    + means match 1 or more times
    $ signifies the end of the string

    Update:
    you can also add [+-]? right after ^ if your numbers can have prefixes. Also, see this page: Is it a number?

    Update 2:
    Yes, there are obvious loopholes, Anonymous Monk, such as decimal numbers (they didn't specify what they were matching exactly. just "numeric values"). See the link above (is it a number?) for more robust solutions.

    Here's a snippet of some nice functions from it:
    sub is_whole_number { $_[0] =~ /^\d+$/ } sub is_integer { $_[0] =~ /^[+-]?\d+$/ } sub is_float { $_[0] =~ /^[+-]?\d+\.?\d*$/ }
    Also, use \z if you wanted to "match the actual end of the string and not ignore an optional trailing newline" (perlre)
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Is Array Element Numeric? (was I wonder...)
by fruiture (Curate) on Aug 22, 2002 at 12:52 UTC

    It doesn't make a difference if it's an array element or a simple scalar, in any case the perlfaq has answered it. See `perldoc -q 'determine.*number'`.

    --
    http://fruiture.de
Re: Is Array Element Numeric? (was I wonder...)
by smitz (Chaplain) on Aug 22, 2002 at 13:24 UTC
    Like fruiture said, perdoc has the answer...

    #!perl -w # use strict; my @array = (100, 'a string!', 45.678, 0.43243, '', "", 0, "another no +rmal string with a carriage return \n"); foreach (@array) { if (/\D/) { print "$_ contains non-digits\n"; } elsif (/^\d+$/) { print "$_ is a whole number\n"; } elsif (/^-?\d+$/) { print "$_ is an integer\n"; } elsif (/^[+-]?\d+$/) { print "$_ is a +/- integer\n"; } elsif (/^-?\d+\.?\d*$/) { print "$_ is a real number\n"; } elsif (/^-?(?:\d+(?:\.\d*)?|\.\d+)$/) { print "$_ is a decimal number\n"; } elsif (/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/) { print "$_ is a C float\n"; } else { print "$_ not a number format I recognise, bud.\n"; } }

    SMiTZ
Re: Is Array Element Numeric? (was I wonder...)
by Bird (Pilgrim) on Aug 22, 2002 at 20:34 UTC
    Personally, I'd put much more faith in theDamian and Abigail's code than my own. I'd use Regexp::Common.
    use Regexp::Common qw /number/; for ('1', 'flip', '-2.3e14', '0.01', 'flop') { /$RE{num}{real}/ and print "Yup, $_ is a number!\n"; }
    -Bird
Re: Is Array Element Numeric? (was I wonder...)
by moodster (Hermit) on Aug 22, 2002 at 12:58 UTC
    Well, you could always perform some kind of numeric operation on your array element and see if performs as you'd expect. If it doesn't, it's probably a string. Try this for example:
    if( abs( $array[ 0 ] ) != 0 ) { # the absolut value of this scalar is not 0, so it's probably not a + string }
    This works with floats as well as really large numbers (like "5.4e+10"), but beware: the absolute value of 0 is 0, so that's a false negative. I'll leave the handling of 0 as an excercise for the reader :)

    Cheers,
    --Moodster

Re: Is Array Element Numeric? (was I wonder...)
by fglock (Vicar) on Aug 22, 2002 at 15:18 UTC
    if ($array[0] eq (0+$array[0])) { # certainly IS a number } else { # might be a "string" number: # might contain extra space, or plus sign, # or extra zeroes, ... # Add more checking here if you need. }