# help understanding ++

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on

Hi,

----------
The auto-increment operator has a little extra builtin magic to it. If
you increment a variable that is numeric, or that has ever been used in
a numeric context, you get a normal increment. If, however, the variable
has been used in only string contexts since it was set, and has a value
that is not the empty string and matches the pattern
/^[a-zA-Z]*[0-9]*\z/, the increment is done as a string, preserving each
character within its range, with carry:

print ++(\$foo = '99');      # prints '100'
print ++(\$foo = 'a0');      # prints 'a1'
print ++(\$foo = 'Az');      # prints 'Ba'
print ++(\$foo = 'zz');      # prints 'aaa'
-----------

Can somebody please explain to me why print ++(\$foo = 'a0');      #
prints 'a1' rather than 'b1', and why print ++(\$foo = 'Az');      #
prints 'Ba' rather than 'Baa', as I would expect from the fourth example.

Or, more general: provide me with some clearer rules...

Thanks, H.

--
Hendrik Maryns

Interesting websites:
www.lieverleven.be    (I cooperate)
www.eu04.com        European Referendum Campaign
aouw.org        The Art Of Urban Warfare

## Re: help understanding ++

The same reason that '10' incremented becomes '11' rather than '21'.

When you increment, you increment the last character unless it is at the
end of the range (either z or 9).  In this case 0 can be incremented and
will become 1.

19++ => 20, not 19 => 200.  You don't add a digit until all the
characters are at the end of their range.
( 99++ => 100, zz++ => aaa)

--
Darren Dunham                                           ddunham@taos.com
Senior Technical Consultant         TAOS            http://www.taos.com /
Got some Dr Pepper?                           San Francisco, CA bay area
< This line left intentionally blank to confuse you. >

## Re: help understanding ++

Darren Dunham schreef:

Sounds logical!
Thanks.

--
Hendrik Maryns

Interesting websites:
www.lieverleven.be    (I cooperate)
www.eu04.com        European Referendum Campaign
aouw.org        The Art Of Urban Warfare

## Re: help understanding ++

--

Seems to me that's not quite right. The following prints '9', not '8d' as I
would expect:

print ++(\$foo = '8c');

Using perl 5.8.6 on Win32.

Cheers,
Rob

## Re: help understanding ++

On Thu, 14 Apr 2005 10:48:25 +1000, "Sisyphus"

Look at the pattern again. The magic autoincrement occurs when all the
letters are before the digits. When the string starts with a digit, Perl
converts it to a number before incrementing.

--
Eric Amick
Columbia, MD

## Re: help understanding ++

Aaah, yes .... I didn't examine the pattern properly. Thanks.

Cheers,
Rob

## Re: help understanding ++

Sisyphus wrote:

This is where auto-numification gets in the way. The special behaviour
of ++ for letter sequences (eg 'a0'++ = 'a1') applies when perl can't
recognise the input to ++ as a number. In numeric context, perl sees
'8c' as 8, so '8c'++ is 9, not '8d'. Reverse it - 'd8' can't be
recognised as a number, so 'd8'++ is 'd9'. This is also why '99'++ is
100, not '9a' :)

MB