Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Why does $string++ work the way it does?

by extremely (Priest)
on Oct 26, 2000 at 11:54 UTC ( [id://38572]=note: print w/replies, xml ) Need Help??


in reply to Why does $string++ work the way it does?

Well, you've discovered that '++' is magic on strings of letters. The magic works on strings like "aaaa" and "aaa111" but on strings like "1111aaaa" you will get 1112. If the string begins with a number then perl will whack off the text and treat it as a number. All the other math ops will do the same for a string that begins with a number.

BTW, perl's definition of a number is pretty complex. "-1e3garg" + "+4ee3" will net you 1004.

$a="-1e3ble"; $b="+4.33eeq"; $c="aaa999"; $d="a9z9z9"; print "a = $a and b = $b\n"; print "c = $c and d = $d\n"; print "a + b = ". $a+$b . "\n"; print "c + d = ". $c+$d . "\n"; print "int(b) = ". int($b) . "\n"; print "++a = ". ++$a . "\n"; print "++b = ". ++$b . "\n"; print "++c = ". ++$c . "\n"; print "++d = ". ++$d . "\n";

try out that code and see what I mean. Note what happens to $c when the numbers flip and notice that $d goes poof. =)

Replies are listed 'Best First'.
Re: Answer: Why does $string++ work the way it does?
by I0 (Priest) on Dec 26, 2000 at 05:23 UTC
    "-1e3garg" + "+4ee3" #gives me -996, not 1004

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (None)
    As of 2024-04-25 03:54 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found