RE: perl 'advancing' variable facility..



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
.



Relevant Pages

  • Re: Ascii characters in a loop
    ... MH> perl has no such beast. ... >> magic increment, have you read it? ... string contexts since it was set, and has a value that is ...
    (comp.lang.perl.misc)
  • Cant Compiling perl5.8.8 on FreeBSD6.2
    ... WITH_DEBUGGING=yes Build perl with debugging support. ... First let's make sure your kit is complete. ... What is the file extension used for shared libraries? ... I'll use sprintf to convert floats into a string. ...
    (comp.unix.bsd.freebsd.misc)
  • Re: use of DBI; I am getting multiple error messages mixed in with ?the correct output.
    ... But I'm not talking about C++ or Java or SQL. ... This is a perl newsgroup. ... to means by a "defined null string", as opposed to an undefined value. ... And where exactly are you getting the idea that the empty string is a ...
    (comp.lang.perl.misc)
  • Re: Perl Strings vs FileHandle
    ... Just wanted to run this through Perl gurus to see if fit is ... testing it every time through the loop. ... The comparison test to the string '1' is superfluous. ... Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers ...
    (comp.lang.perl.misc)
  • Re: Working with Source Code to Insert Copyright Statements as a Header
    ... Paul Lalli wrote: ... Where could I go to find some samples of PERL code that do the ... ascii english text, etc etc etc) at the top of the file, from a string: ... `perldoc Tie::File` at the command line to read its documentation. ...
    (comp.lang.perl.misc)