If it acts just like a number as far as Perl is concerned, then it's a number as far as I'm concerned.
You can let Perl tell you what it thinks then...
#!perl
use strict;
use warnings;
sub seems_like_number {
my $thing = shift;
use warnings qw/FATAL all/; # Promote warnings to fatal, so
# they can be trapped. The effect is
# lexically scoped.
eval {
$thing += 0;
};
return $@ ? 0 : 1;
}
while ( <DATA> ){
chomp;
if( seems_like_number( $_ ) ) {
printf "%10s seems like a number.\n", $_;
} else {
printf "%10s doesn't seem like a number.\n", $_;
}
}
__DATA__
1234
1.234
-2.345
1-1+2
0.032
.321
ASDF
ASDF1234
1234ASDF
The first line after __DATA__ is intentionally blank, to test whether or not an empty string will qualify as a number... and of course it won't.
Ok, now if Perl thinks it's not a number, you'll know about it, and if Perl doesn't object to it being considered a number, you'll know that too.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|