autoincrement strangeness - please help!

I am learning Perl and was using the Wrox Learning Perl book that I
found on the web. The author was explaining the autoincrement operator
and try as I might I can't understand this behaviour. The line of code
read...

$a = "9z"; print ++$a, "\n";

Aparently Perl reads in the ranges a-z, A-Z, 0-9 and if it caps off a
range then it will add a digit on the leftmost side and go back to the
beginning of a range ...but i think I misunderstand this as I was
expecting...

munnki@localhost:> 00a

to be output to the shell, as opposed to

munnki@localhost:> 10

i thought that the 9 will have reached it's max and become two zeros
(the start of the next range) and the z would, having reached it's
upper bound, have become an 'a'.

I hope I don't sound too stupid and I know this probably isn't the
most important aspect of the language..but i don't fully understand
this...could someone explain...

Advertisements

Munnki wrote:
> I am learning Perl and was using the Wrox Learning Perl book that I
> found on the web. The author was explaining the autoincrement operator
> and try as I might I can't understand this behaviour. The line of code
> read...
>
> $a = "9z"; print ++$a, "\n";

[Problem with auto-increment magic]

"9z" does not match the pattern "/^[a-zA-Z]*[0-9]*\z/", therefore the magic
is switched off, and the string "9z" is treated as a number which happens to
have the numerical value 9.
Try "z9" instead and you will get the result you are expecting.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!