Simple Question about Scalars

D

David Young

Hello all,

Quite new to Perl and I have a really simple question .... I have a scalar
varible which could contain a string of a numberic value ....

How can I tell if a scalar contains a string or a value?

Yours,

David
 
P

Paul Lalli

David said:
Hello all,

Quite new to Perl and I have a really simple question .... I have a scalar
varible which could contain a string of a numberic value ....

How can I tell if a scalar contains a string or a value?

First, a string is a value. A number is also a value.

The generic answer is "use a regular expression". The more specific
answer depends on exactly what kind of numbers you want.

Purely digits?
m/^\d+$/
A possible negative sign?
m/^-?\d+$/
A decmial, followed by more digits?
m/^-?\d+(?:\.\d+)?$/
An exponent (with only positive powers of 10)?
m/^-?\d+(?:\.\d+)?(?:e\d+)?$/i

As you can see, this quickly becomes complicated. For that reason,
there are several tools available. First, check out the relevant FAQ:
perldoc -q determine
"How do I determine whether a scalar is a number/whole/integer/float?"

Then check out the function it mentions, POSIX::strtod
perldoc POSIX

Finally, search CPAN for a couple helper modules:
http://search.cpan.org/~jhi/String-Scanf-2.1/lib/String/Scanf.pm
http://search.cpan.org/~abigail/Regexp-Common-2.120/lib/Regexp/Common.pm

Paul Lalli
 
D

Damian James

Quite new to Perl and I have a really simple question .... I have a scalar
varible which could contain a string of a numberic value ....
How can I tell if a scalar contains a string or a value?

Have a look over the perldata man page, in the section headed "scalar
values". This contains a general discussion of the issue, and suggests
a simple way to tell, and some more involved ways.

perldoc perldata

should show you this page, otherwise see the documentation for your
distribution.


--Damian
 
V

Veli-Pekka Tätilä

David said:
Quite new to Perl and I have a really simple question
How can I tell if a scalar contains a string or a value?
You mean string or a number. You could use regular expressions as suggested
by others.

One thing that no-one's yet mentioned, at least when I started writing this,
is the Scalar::Util module. I'm not sure about any other versions of Perl
but if you are running Active State Perl, you'll already have the module.

Here's how to determine if the first command-line argument is numeric:

use strict;
use warnings;
use English;
use Scalar::Util qw(looks_like_number);

local $OUTPUT_RECORD_SEPARATOR = "\n"; # Auto-print new-lines after prints.
my $scalar = $ARGV[0];

if(not defined $scalar)
{ # User didn't give any argument at all.
print "Please enter something after $PROGRAM_NAME.";
} # if
elsif(looks_like_number $scalar)
{ # We're dealing with a number here.
print "$scalar looks like a number to me.";
} # elsif
else
{ # Not a number => (NaN)
print "$scalar - that's no number.";
} # else

Points worthy of note:

1. The qw operator makes a list out of space delimited items this time a
single scalar. It follows the use statement and is not separated by a comma.
That idiom can be used to export function names directly in your name space.
If you didn't include the qw bit you would have to say:
Scalar::Util::looks_like_number()
which reminds me of Java in a negative way.

2. The English module let's one use readable variable names such as
$INPUT_RECORD_SEPARATOR. Here we've redefined the output record separator to
give a new-line after each print statement (output record or line if you
will). This ensures output behavior typical of DOS programs, that is two
line breaks before the prompt c:\> or whatever.

3. The looks_like_number function seems to work very well and might have a
faster version coded in C. SO I see no reason for having to match regular
expressions against numberness if any kind of number will do. IF you are
looking for particular kinds of numbers or numbers having certain properties
things will be different.

Hope this can be of help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top