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)