in reply to How can I test for the representation of an integer?

#!/usr/bin/perl use strict; use warnings; use Scalar::Util qw(looks_like_number); my @list = qw(1 "1" "1o"); foreach my $foo (@list) { if( looks_like_number( $foo ) ) { print "number\n"; }else{ print "string\n"; } }

Just use looks_like_number from Scalar::Util

Replies are listed 'Best First'.
Re^2: How can I test for the representation of an integer?
by BrowserUk (Patriarch) on Apr 25, 2016 at 13:31 UTC

    Trouble with that is it treats both 1, and '1' (and 1.0 and '1.0') as looking like numbers (albeit with strangely different values of true)

    use Scalar::Util qw(looks_like_number);; [0] Perl> print looks_like_number( $_ ) for 1, '1', 1.0, '1.0', '1o';; 4352 1 8704 5 0

    But the OP wants to distinguish between 1 stored internally as an integer; and '1' stored internally as a string.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.