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:
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.
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.
9z
does 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 Zz
should become Aa
(unless you're thinking of modular arithmetic, but this isn't). It becomes AAa
through this process:
z
wraps around to a
. Increment the previous character.Z
wraps around to A
. There is no previous character, so add the first one from this range, which is another A
.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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With