Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Simple Question about Scalars

Reply
Thread Tools

Simple Question about Scalars

 
 
David Young
Guest
Posts: n/a
 
      08-09-2005
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


 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      08-09-2005
David Young wrote:
> 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-S...tring/Scanf.pm
http://search.cpan.org/~abigail/Rege...gexp/Common.pm

Paul Lalli

 
Reply With Quote
 
 
 
 
Damian James
Guest
Posts: n/a
 
      08-09-2005
On Tue, 9 Aug 2005 18:14:32 +0100, David Young said:
> 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
--
@:=grep!(m!$/|#!..$|),split//,<DATA>;@;=0..$#:;while($:=@{$;=rand
$:--,@;[$;,$:]=@;[$:,$;]while$:;push@|,shift@;if$;[0]==@|;select$,,
$,,$,,1/80;print qq x\bxx((@;+@|)*$|++),@:[@|,@;],!@;&&$/} __END__
Just another Perl Hacker,### http://home.pacific.net.au/~djames.hub
 
Reply With Quote
 
Veli-Pekka Tštilš
Guest
Posts: n/a
 
      08-10-2005
David Young wrote:
> Quite new to Perl and I have a really simple question

Nothing wrong with that <wink>. I'm a bit of a newbie myself and hope these
kind of simple questions are all right in here.

> 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.

--
With kind regards Veli-Pekka Tštilš ((E-Mail Removed))
Accessibility, game music, synthesizers and programming:
http://www.student.oulu.fi/~vtatila/


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
TypeError: only length-1 arrays can be converted to Python scalars. nure123@gmail.com Python 2 02-28-2008 08:05 PM
Scipy: vectorized function does not take scalars as arguments ago Python 3 05-25-2006 09:22 AM
naming convention for scalars, lists, dictionaries ... beliavsky@aol.com Python 8 03-01-2005 10:11 AM
Lexical filehandles must be simple scalars (not eg hash elements) ? Richard Gration Perl Misc 3 01-11-2005 06:38 PM
Inheriting scalars Ray Gardener C++ 7 06-10-2004 01:23 AM



Advertisments