in reply to
Re: A function to determine if a string is numeric
in thread A function to determine if a string is numeric
$ perl -E'
print "Enter a number or string: ";
my $s = <STDIN>;
chomp $s;
if ( $s =~ /^[0-9,.E]+$/ ) {
say "$s is a number\n";
} else {
say "$s may be a string\n";
}
'
Enter a number or string: ,,,
,,, is a number
$ perl -E'
print "Enter a number or string: ";
my $s = <STDIN>;
chomp $s;
if ( $s =~ /^[0-9,.E]+$/ ) {
say "$s is a number\n";
} else {
say "$s may be a string\n";
}
'
Enter a number or string: EEE
EEE is a number
$ perl -E'
print "Enter a number or string: ";
my $s = <STDIN>;
chomp $s;
if ( $s =~ /^[0-9,.E]+$/ ) {
say "$s is a number\n";
} else {
say "$s may be a string\n";
}
'
Enter a number or string: -777
-777 may be a string
If you are going to use a regular expression then you should probably read the FAQ: How do I determine whether a scalar is a number/whole/integer/float?