in reply to Changing a number to a string


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

Replies are listed 'Best First'.
Re: Re: Changing a number to a string
by merlyn (Sage) on Feb 02, 2001 at 21:14 UTC