Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Number as string

by manoj_speed (Prior)
on Mar 13, 2009 at 12:11 UTC ( #750381=perlquestion: print w/replies, xml ) Need Help??

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

Hi friend

I declared a variable in my program. I assigned a numeric value(Ex.10) to that variable. How can I make my perl program to treat that numeric value as a string?. ( It should not add the values when I use the plus(+) operator )

Replies are listed 'Best First'.
Re: Number as string
by bellaire (Hermit) on Mar 13, 2009 at 12:15 UTC
    It's not possible. In Perl, unlike most languages, the operators force their context on their operands. If you use + on any values, they will be converted into numerics. What exactly are you trying to accomplish?
      This is right. Everything is a string until you do something that puts it into a numeric context. Here's a little trick that "deletes" leading zeroes.
      #!/usr/bin/perl -w use strict; my $num="000123"; print "$num\n"; $num += 0; print "$num\n"; _END_ prints: 000123 123
      If you are say comparing things that are supposed to be numbers, then use the numeric compare rather than a string compare! Perl doesn't know "000123" is a number until you say it is. A <,>, etc. will trigger this numeric conversion. Just something to watch out for when you are doing sorts, etc. The ascii sort order is different than the numeric sort order, see this...
      my $a="99"; my $b="111"; if ($a gt $b) {print "$a is greater than $b";} #prints 99 is greater than 111
Re: Number as string
by lostjimmy (Chaplain) on Mar 13, 2009 at 13:32 UTC

    If you want to concatenate two variables together, either use the concatenation operator, or just interpolate them.

    my ($var1, $var2) = ("variable = ", 10); # concatenate print $var1 . $var2; # interpolate print "$var1$var2";
Re: Number as string
by misterwhipple (Monk) on Mar 13, 2009 at 13:35 UTC
    The + operator is always about addition of numbers. If you want to concatenate a string value, use ".".

    From Programming Perl:

    $a = 123; $b = 456; print $a + $b; # prints 579 print $a . $b; # prints 123456

    Your left-hand veeblefetzer is calibrated to the wrong Frammistan coefficient. Pass me that finklegruber.

Re: Number as string
by JavaFan (Canon) on Mar 13, 2009 at 14:25 UTC
    Except in a few isolated cases, Perl doesn't dispatch operators on their argument types. In general, it's the operator that determines what happens, and the operands adjust themselves. Other languages have operators adjust themselves to their operands, but not Perl. (There will be type based dispatch in Perl6. But that an "other language"). In Perl, if you want to concatenate values, you have to use the concatenation operator: ..

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2021-10-26 04:43 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (90 votes). Check out past polls.