in reply to Re3: How to determine a variable value is a number
in thread How to determine a variable value is a number
New version of the code that still doesn't work.
#!/usr/bin/perl -w use strict; sub i_am_a_number { my $val = shift; no warnings; # += 0 on a string sets off a warning # make sure that zero is counted as a number # ($_ += 0) alone fails on zero. if ($val =~ /^\d+$/ || ($val += 0)) { return 1; } return 0; } ############################################################# # take the sub on a test spin my @list = ( # Zeros 0.00, .00, 0. , 0, -0, 00, # Integers 035, 35, +19, "045 ", " 045", -2, #Floats .15, 5.5, -34.530, # Not numbers " 0 1 ", " 0 0 ", " 24 0 ", " 15hi ", "2isnotanumber", "hi!" ); for (@list) { if (i_am_a_number($_) || i_am_a_number($_)) { print "$_ is a number!\n"; } else { print "$_ is not a number!\n"; } }
()-() \"/ `
|
|---|