Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^4: How the auto-increment operator works?

by zapdos (Sexton)
on Aug 02, 2020 at 00:20 UTC ( [id://11120218]=note: print w/replies, xml ) Need Help??


in reply to Re^3: How the auto-increment operator works?
in thread How the auto-increment operator works?

if I do my $foo = "Be"; print ++$foo; it prints "Bf".

But if I do my $foo = "Bz"; print ++$foo; it prints "Ca". I'm not getting this at all.

Replies are listed 'Best First'.
Re^5: How the auto-increment operator works?
by LanX (Saint) on Aug 02, 2020 at 00:28 UTC
    Why are you so interested in this?

    Nobody really cares as long as there is no repetition. :)

    Anyway think of this old counting machines with numbers on wheels, if one of the wheels changed from 9 to 0 it also incremented the one the the left, and so on. So 19++ becomes 20.

    That's totally the same thing, only difference is that we have three different kind of wheels.

    They are either

    • digits 0-9
    • lowercase a-z
    • uppercase A-Z

    > if I do my $foo = "Bz"; print ++$foo; it prints "Ca".

    "z" is at the upper limit like 9, when changing to the start "a" it has to increment the next position, but within it's symbol group. "C" follows "B" like 2 followed 1 when incrementing 19.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      Sorry, I'm not trolling, I'm just a dumbfuck... can you please explain to me why my $foo = "Be"; print ++$foo; prints "Bf" and not "Cf"?

        For essentially the same reason that ++75 is 76 and not 86:

        c:\@Work\Perl\monks>perl -wMstrict -le "my $s = '75'; printf qq{++'$s' -> }; ++$s; print qq{'$s'}; " ++'75' -> '76'
        Why would a "carry" ("B" -> "C") be generated by incrementing from "e" to "f"?


        Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11120218]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-25 17:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found