tags:

views:

87

answers:

2

Possible Duplicate:
Autoincrementing letters in Perl

I am trying to understand Perl's pre-increment operator. For every different variable, I find the pre-increment operator behavior strange in Perl.

Example :

#!/usr/bin/perl
$a = "bz";
print ++$a, "\n";

RESULT: ca

#!/usr/bin/perl
$a = "9z";
print ++$a, "\n";

RESULT: 10
Shouldn't the result be 10a?

#!/usr/bin/perl
$a = "bxz"; 
print ++$a, "\n";

RESULT: bya
Shouldn't the result be cya?

+8  A: 

Shouldn't the result be 10a?

No, because the magical increment behavior does not apply to values that have letters following numbers. Those are simply converted to numbers and are incremented as numbers. Specifically, "magical increment" can only happen to a value matching /^[a-zA-Z]*[0-9]*\z/, per perlop.

Shouldn't the result be cya?

No. There's no reason for a second carry. The "z" wraps to "a", and the preceding "x" is incremented to become "y", but that didn't wrap, so there's no further carry.

hobbs
+4  A: 

see Autoincrementing-letters-in-perl

For more detail see perlop

Nikhil Jain