in reply to Re^3: number array with space
in thread number array with space

Hey

one more question that how to check whether the element in an array is a number or not . is there any function in perl

Replies are listed 'Best First'.
Re^5: number array with space
by davido (Cardinal) on Jun 20, 2013 at 07:00 UTC

    See Scalar::Util, and its function "looks_like_number". The module is core, meaning it ships with Perl.


    Dave

Re^5: number array with space
by 2teez (Vicar) on Jun 20, 2013 at 07:09 UTC

    See looks_like_number in Scalar::Util like thus:

    use Scalar::Util qw(looks_like_number); for(qw(1 perl 25 monks)){ looks_like_number $_ ? print $_," is a Number",$/ : print $_," is not a Number",$/; }
    Update:
    Awooooshhhh! davido, beat me to it.....

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me

      Yeah, but I left out the example, which you thoughtfully provided. ...or I left it as an exercise for the reader to figure out. ;)


      Dave

      hey

      what i want to do is that check whether the array cell has a number or not , if yes execute my block of code else not i.e.

      if( $array[$i] == number ) { my block of code ; }
        Simply go for regex then:
        if ($array[$i] =~ /\d+/){ <your code> }