autoincrement strangeness - please help!

M

Munnki

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...

thanx
munnki
 
J

Jürgen Exner

Munnki said:
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.

For further details please see "perldoc perlop".

jue
 
M

Munnki

thanx Jurgen - i need to start learning about regular expressions and
such - i have been delaying it as all those strange symbols hurt my
eyes :(|)
thanx again
munnki :(|)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top