RE: perl 'advancing' variable facility..
- From: t.baetzler@xxxxxxxxxx (Thomas Bätzler)
- Date: Wed, 25 May 2005 13:42:44 +0200
Vineet Pande <pande_vineet@xxxxxxxxxxx> asked:
> I don't understand the way perl 'advances' the variables
> along the range a-z, A-Z, and 0-9.
This is described in the perlop manpage:
[...]
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'
[...]
> For example from a book
>
> $a = "A9"; print ++$a, "\n";
>
> gives B0 as expected,
>
> but,
> $a = "Zz"; print ++$a, "\n";
>
> gives AAa.
>
> Why?? why not Aa only!
Think about how this works. Perl first look at the 'z' and turns
it into an 'a'. It also knows that it has a carry-over digit, so
it next looks at the 'Z'. This is turned into an 'A', and there
is a carry-over again, so that becomes the leading 'A'.
> also $a = "9z"; print ++$a, "\n";
>
> gives 10.
> and why not 0a?
Because "9z" is a number with trailing garbage, which is silently
discarded during transformation to a number. The incrementing of
letters only works for strings that start with letters - see the
quoted text above.
HTH,
Thomas
.
- Prev by Date: Re: output in a single line
- Next by Date: RE: Can't use subscript in angle brackets
- Previous by thread: Re: perl 'advancing' variable facility..
- Index(es):
Relevant Pages
|