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.
| [reply] |
|
I don't get it. Can you explain more thoroughly please?
| [reply] |
|
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
| [reply] |
|
Re: How the auto-increment operator works?
by ikegami (Patriarch) on Jul 31, 2020 at 13:47 UTC
|
$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);
| [reply] [d/l] [select] |
|
Thank you so much for helping me, ikegami. ;-)
| [reply] |
Re: How the auto-increment operator works?
by LanX (Saint) on Jul 31, 2020 at 07:37 UTC
|
| [reply] [d/l] [select] |
|
| [reply] [d/l] [select] |
|
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.
| [reply] |
|
|
|
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
| [reply] [d/l] [select] |
|
What's the range to get zz++ —> aaa please?
| [reply] |
|
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: <%-{-{-{-<
| [reply] [d/l] [select] |
|
Re: How the auto-increment operator works?
by zapdos (Sexton) on Aug 04, 2020 at 23:36 UTC
|
| [reply] [d/l] |
|
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]
| [reply] [d/l] [select] |
|
Thank you very much brother ;-)
| [reply] |
|
>
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.
| [reply] |
|
Thank you very much again bro ^_^
| [reply] |