Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

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

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


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

Thank you very much. Now I have one more question: why my $foo = "Zz"; print ++$foo; prints "AAa" and not "AAaa"? Since there's two "z" characters and when one "z" character is incremented it produces "aa".

Replies are listed 'Best First'.
Re^9: How the auto-increment operator works?
by LanX (Saint) on Aug 02, 2020 at 02:13 UTC
    > > > If a new position is needed it'll adjust to the leftmost group.

    > my $foo = "Zz"; print ++$foo; prints "AAa"

    The A is a new position the next position to the right - which is causing the "carry" - is an uppercase character A.

    > when one "z" character is incremented it produces "aa".

    Again the left a is a new position, and the position to the right is a lowercase character.

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

      ... I'm not trolling ...

      Hmmm... Starting to question this.


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

      "The A is a new position the next position to the right - which is causing the "carry" - is an uppercase character."

      "Again the left a is a new position, and the position to the right is a lowercase character."

      I don't get either. This is killing me inside. And AnomalousMonk, no, I'm not trolling I swear for God.

        Perhaps the best thing to do is to remember that in "magic" string incrementation, digit/lowercase/uppercase characters increment from right to left, wrap within themselves, and generate a "carry" into the next left column (or a new left column character if it's already the leftmost (update: and see LanX's reply concerning the "type" of a new column)) when they wrap:

        increment left col ("carry") <- '0' -> '1' -> .. '9' (digits) ^ | | | +----------------+ increment left col ("carry") <- 'a' -> 'b' -> .. 'z' (lower case) ^ | | | +----------------+ increment left col ("carry") <- 'A' -> 'B' -> .. 'Z' (upper case) ^ | | | +----------------+
        String incrementation is pretty stable. I've never known it to change and I doubt it ever will (to preserve backward compatibility), so the best thing I can suggest is to experiment. Whatever results you get are the way "magic" string incrementation works.
        c:\@Work\Perl\monks>perl -e "my $s = 'zZ9'; print qq{'$s' -> }; ++$s +; print qq{'$s'};" 'zZ9' -> 'aaA0' c:\@Work\Perl\monks>perl -e "my $s = 'Zz9'; print qq{'$s' -> }; ++$s +; print qq{'$s'};" 'Zz9' -> 'AAa0' c:\@Work\Perl\monks>perl -e "my $s = 'Zzzz9'; print qq{'$s' -> }; ++ +$s; print qq{'$s'};" 'Zzzz9' -> 'AAaaa0' c:\@Work\Perl\monks>perl -e "my $s = 'zzzz9'; print qq{'$s' -> }; ++ +$s; print qq{'$s'};" 'zzzz9' -> 'aaaaa0' c:\@Work\Perl\monks>perl -e "my $s = 'ZZZZ9'; print qq{'$s' -> }; ++ +$s; print qq{'$s'};" 'ZZZZ9' -> 'AAAAA0'


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

        > "The A is a new position the next position to the right - which is causing the "carry" - is an uppercase character."

        > I don't get either

        Please see Carry (arithmetic)

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-20 06:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found