http://qs321.pair.com?node_id=11120100

zapdos has asked for the wisdom of the Perl Monks concerning the following question:

Auto increment and Auto decrement

With the example print ++($foo = "Az"); # prints "Ba" The logic in my head is that it printed uppercase "B" because it's the letter that comes after the uppercase "A", and it printed lowercase "a" because it's what comes after lowercase "z" going back to the first letter of the alphabet that's "a" since "z" is the lastest.

So why print ++($foo = "zz"); prints "aaa"? It makes no sense to me. Shouldn't it print just "aa" since there's two z characters?

2020-08-04 Athanasius shortened link.

Replies are listed 'Best First'.
Re: How the auto-increment operator works?
by jcb (Parson) on Jul 31, 2020 at 01:57 UTC

    Think of it as counting in a weird base-26: 99++ —> 100, Ay++ —> Az, Az++ —> Ba, so zz++ —> aaa.

      I don't get it. Can you explain more thoroughly please?

        When you are counting in base-10 and you reach 99, you add a leading "1" instead of wrapping back to only "00". The magic autoincrement does the same with letters: when you reach the end of the alphabet, it adds a leading "a" while wrapping back instead of repeating a previous value.

        This is also why "z" becomes "aa":

        $ perl -e '$X = "z"; print $X, "\n"; $X++; print $X, "\n";'
        z
        aa
        
Re: How the auto-increment operator works?
by ikegami (Patriarch) on Jul 31, 2020 at 13:47 UTC

    For the same reason that the following results in 100 instead of 00 (0).

    $x = 99; ++$x;

    If you exceed the range of the second position, it carries into the third position. To keep it to two digits, you need to take explicit action.

    $x = 99; ++$x; $x %= 100;
    $x = 'zz'; ++$x; $x = substr($x, -2);
      Thank you so much for helping me, ikegami. ;-)
Re: How the auto-increment operator works?
by LanX (Saint) on Jul 31, 2020 at 07:37 UTC
      Please, why my $foo = "Zz"; print ++$foo; prints "AAa" and not "Aaa"?

      And why $my foo = "c9"; print ++$foo; prints "d0" and not "c10"?

        Looks like you have three groups of symbols - digits , lower and uppercase characters - and every position stays within its group.

        If a new position is needed it'll adjust to the leftmost group.

        This makes sense to me, because the structure of a log_89ABCacd.txt file will always look the same, with only the numbers growing.

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

Re: How the auto-increment operator works?
by perlfan (Vicar) on Jul 31, 2020 at 02:26 UTC
    foreach my $x ('aa' .. 'bb') { print qq{$x\n} }
    yields:
    aa ab ac ad ae af ag ah ai aj ak al am an ao ap aq ar as at au av aw ax ay az ba bb
      What's the range to get zz++ —> aaa please?

        c:\@Work\Perl\monks>perl -wMstrict -le "print for 'zx' .. 'aac'; ;; my $s = 'zx'; print $s++ for 0 .. 5; " zx zy zz aaa aab aac zx zy zz aaa aab aac


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

Re: How the auto-increment operator works?
by zapdos (Sexton) on Aug 04, 2020 at 23:36 UTC
    Wait please help me with this again, I don't get why print ++($foo = "Zy"); prints "Zz" and not "Az". Isn't the uppercase "Z" a limit?

    And I don't get what "wrap" means either because english is not my native language.

      It's similar to
      print ++($foo = 98)
      which returns 99. Similarly, in "Zy", it first tries to increment from the right, and y -> z is a valid increment which finishes the operation.

      The next word would be AAa, because "z" goes to "a" with a "carry flag", so we need to increment "Z", which goes to "A" with a carry flag, and as there's nothing else to increment to the left, we add "A" (the case is copied from the previous letter).

      Wrap means "to start the round from the beginning again" here.

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
        Thank you very much brother ;-)
      > And I don't get what "wrap" means

      The "positions" rotate like in a mechanical calculator, only difference some have characters instead of digits.

      Whenever one wheel finished a full rotation it'll increment the next one to the left.

      Anyway we told you already and I'm tired of this now.

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

        Thank you very much again bro ^_^