Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Changing a number to a string

by Anonymous Monk
on Jan 25, 2001 at 22:05 UTC ( #54307=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How do I force PERL to see "0" as a string I believe that it is seeing it as a number. Is there like a parsestring($foo) or something?

Replies are listed 'Best First'.
Re: Changing a number to a string
by jepri (Parson) on Jan 25, 2001 at 22:14 UTC
    You don't have to worry about that, Perl does it for you.
    If you use a numerical compare:

    if ($var == 0 )
    it'll be a number.

    If you use a string compare:

    if ($var eq "0")
    then it'll be a string.

    This actually took me quite a long time to get the hang of, sadly.

    ____________________
    Jeremy
    I didn't believe in evil until I dated it.

Re: Changing a number to a string
by davorg (Chancellor) on Jan 25, 2001 at 22:12 UTC

    (it's "Perl" not "PERL")

    Can you give us a bit more context. Like a smallest test case sample of code. Perl normally does the Right Thing in cases like these, but it'll be easier to see what's going on with a snippet of code.

    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

Re: Changing a number to a string
by jynx (Priest) on Jan 26, 2001 at 05:01 UTC

    wow

    i'm amazed no one's brought up merlyn's solution yet:

    "$_ is string\n" if (~$_ & $_) ne '0';
    This is taken verbatim from Effective Perl Programming. Not sure if it will work correctly when dealing with a "0" string, but it's worth testing (give me a couple minutes :)

    jynx

    Update:that's cool! if you declare:

    my $string = "0"; my $num = 0; # then we: print "string: $string is a string.\n" if ((~$string & $string) ne '0' +); print "num: $num is a string.\n" if ((~$num & $num) ne '0');
    Notice it only outputs the one that looked like a string. That merlyn's a genius.

    For more information on why this works read page 248 of Effective Perl Programming...

Re: Changing a number to a string
by CiceroLove (Monk) on Jan 26, 2001 at 01:40 UTC
    Easiest thing I do is just:
    $foo = 0 . "";
    This makes Perl kick in with the Right Thing. In converse, you can also
    $foo = "2345" + 0;
Re: Changing a number to a string
by dsb (Chaplain) on Jan 25, 2001 at 23:13 UTC
    It could also have something to do with the way you initialize it.
    # initializing like this will represent '0' as a number $foo = 0; # by double quoting it, the '0' should be seen as a string $foo = "0";
    - kel -

      How would you know if it's being seen as a string or a number, though? Because once you try to test that, Perl will just Do The Right Thing anyway.

      $foo = 0; # Declaring as a number if ($foo eq '0') { print "Oops, it's a string!\n"; } $foo = "0"; # Declaring as a string if ($foo == 0) { print "Oops, now it's a number!\n"; }

      Bottom line: Perl will always Do The Right Thing when it comes to worrying about stringifying numbers or number-fying strings.

        FYI, I think the "standard" hack for seeing if a scalar has been used in a string context is ^, but it isn't a very good hack.

        I mean to write a module that exposes what types of values a scalar already has cached as well as nifty calls like looks_like_number().

                - tye (but my friends call me "Tye")

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://54307]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2021-07-25 20:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?