The stupid question is the question not asked PerlMonks

### Why does \$string++ work the way it does?

by spaz (Pilgrim)
 on Oct 26, 2000 at 04:33 UTC Need Help??
Contributed by spaz on Oct 26, 2000 at 04:33 UTC
Q&A  > strings

#### Description:

The following code produces some bizzare output. Apparently, "a"++ = "b", and "z"++ = "aa". But if I ever try to convert \$string to a number "\$number = (int \$string)" I get 0. Anybody know why? --snip!--
```#!/usr/bin/perl

my \$string = "a";

for( \$i = 0; \$i < 64; \$i++ ) {
\$string++;
print "string = '\$string'\n";
}
--snip!--

 Answer: Why does \$string++ work the way it does?contributed by extremely 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"; [download]``` try out that code and see what I mean. Note what happens to \$c when the numbers flip and notice that \$d goes poof. =) Answer: Why does \$string++ work the way it does?contributed by Fastolfe ++ is magic in that it knows how to deal with both numbers and strings. With numbers, it behaves just like a numeric increment, but since Perl is smart, it assumes that if you are ++'ing a string, that you want to change the letters around a bit. The alternative would be to convert it to a number (0) and increment that (1), which doesn't make a lot of sense if you start off with a string, so why not make something useful out of it? Note that this is the way that ++ differs from + and +=. The latter two operations require another argument, which can't really be anything but a number, which would convert your string to numeric form first. So if you really do want to end up with a 1 and start from a string, just use +1 or +=1 to get it. Answer: Why does \$string++ work the way it does?contributed by davido The ++ operator can act upon both numbers and strings in a predictable fashion. With strings, 'A' increments to 'B', and 'Z' to 'AA'. ...and so on. The magic of the ++ operator with respect to strings also exherts itself by providing the .. operator (range operator) with similar magic. Try this at home... ```my \$x = 1; my \$b = 'A'; \$x++; # \$x now equals 2. \$b++; # \$b now equals 'B'. my @numbers = (1..10); # @numbers now contains 1, 2, 3, .. 10. my @letters = ('A'..'Z'); # @letters now contains A, B, C, .. Z. my @more = ('A'..'AZ'); # @more now contains A, B, .. Z, AA, AB, ..AZ. [download]``` It seems that ++ magic, and .. magic, are related. Don't be fooled into expecting -- to be magic too though, it isn't. Neither can you use the .. operator alone to create descending lists. ```my \$b = 'Z'; \$b--; # You don't get Y. my @array = (10..1); # You don't get 10, 9..1. my @letters = ('Z'..'A'); # You don't get the letters reversed. [download]``` Have fun with ++ and .. but take care not to expect magic from --. -- is for numbers.

• Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
• Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
• Read Where should I post X? if you're not absolutely sure you're posting in the right place.
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
• You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
 For: Use: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2018-06-21 09:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Should cpanminus be part of the standard Perl release?

Results (117 votes). Check out past polls.

Notices?