Simple Question about Scalars

Discussion in 'Perl Misc' started by David Young, Aug 9, 2005.

  1. David Young

    David Young Guest

    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
    David Young, Aug 9, 2005
    #1
    1. Advertising

  2. David Young

    Paul Lalli Guest

    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-Scanf-2.1/lib/String/Scanf.pm
    http://search.cpan.org/~abigail/Regexp-Common-2.120/lib/Regexp/Common.pm

    Paul Lalli
    Paul Lalli, Aug 9, 2005
    #2
    1. Advertising

  3. David Young

    Damian James Guest

    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
    Damian James, Aug 9, 2005
    #3
  4. 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ä ()
    Accessibility, game music, synthesizers and programming:
    http://www.student.oulu.fi/~vtatila/
    Veli-Pekka Tätilä, Aug 10, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ray Gardener

    Inheriting scalars

    Ray Gardener, Jun 9, 2004, in forum: C++
    Replies:
    7
    Views:
    419
    Ray Gardener
    Jun 10, 2004
  2. Replies:
    8
    Views:
    314
    Paul Boddie
    Mar 1, 2005
  3. ago
    Replies:
    3
    Views:
    322
  4. Replies:
    2
    Views:
    9,019
    Steve Holden
    Feb 28, 2008
  5. Richard Gration
    Replies:
    3
    Views:
    65
    Todd W
    Jan 11, 2005
Loading...

Share This Page