I do not understand autoincrementing letters in Perl.
This example seems perfectly understandable:
$a = 'bz'; ++$a;
ca #output
b gets incremented to c. There is nothing left for z to go to, so it goes back to a (or at least this is how I see the process).
But then I come across statements like this:
$a = 'Zz'; ++$a;
AAa #output
and:
$a = '9z'; ++$a;
10 #output
Why doesn’t incrementing Zz return Aa? And why doesn’t incrementing 9z return 0z?
Thanks!
To quote perlop:
The ranges are 0-9, A-Z, and a-z. When a new character is needed, it is taken from the range of the first character. Each range is independent; characters never leave the range they started in.
9zdoes not match the pattern, so it gets a numeric increment. (It probably ought to give an “Argument isn’t numeric” warning, but it doesn’t in Perl 5.10.1.) Digits are allowed only after all the letters (if any), never before them.Note that an all-digit string does match the pattern, and does receive a string increment (if it’s never been used in a numeric context). However, the result of a string increment on such a string is identical to a numeric increment, except that it has infinite precision and leading zeros (if any) are preserved. (So you can only tell the difference when the number of digits exceeds what an IV or NV can store, or it has leading zeros.)
I don’t see why you think
Zzshould becomeAa(unless you’re thinking of modular arithmetic, but this isn’t). It becomesAAathrough this process:zwraps around toa. Increment the previous character.Zwraps around toA. There is no previous character, so add the first one from this range, which is anotherA.The range operator (
..), when given two strings (and the left-hand one matches the pattern), uses the string increment to produce a list (this is explained near the end of that section). The list starts with the left-hand operand, which is then incremented until either:It returns a list of all the values. (If case 2 terminated the list, the final value is not included in it.)