Thanks all for replies. I've been doing all I could to follow the logic here, but I think I've got it.
reverse is a
chIdx is 0
a.png -> b.png
reverse is z
chIdx is 0
z.jpg -> aa.jpg
reverse is za
chIdx is 0
chIdx is 1
az.png -> ba.png
reverse is zaz
chIdx is 0
chIdx is 1
zaz.png -> zba.png
ott.jpg -> ptt.jpg
reverse is zzz
chIdx is 0
chIdx is 1
chIdx is 2
zzz.tiff -> aaaa.tiff
oi.png -> pj.png
reverse is cba
chIdx is 0
abc.jpg -> abd.jpg
$
sub start_magick {
use strict;
use warnings;
use 5.010;
printf "%s -> %s\n", $_, next_file($_)
for qw/a.png z.jpg az.png zaz.png ott.jpg zzz.tiff oi.png abc.jp
+g/;
}
sub next_file {
use strict;
use warnings;
use 5.010;
my ($previous) = @_;
my ( $name, $ext ) = $previous =~ /(.*)\.(.*)/;
if ( $name !~ tr/ilo/jmp/ ) {
$name = reverse $name;
say "reverse is $name";
for my $chIdx ( 0 .. length($name) - 1 ) {
say "chIdx is $chIdx";
substr( $name, $chIdx, 1 ) =~
tr/abcdefghjkmnpqrstuvwxy/bcdefghjkmnpqrstuvwxyz/
and last;
substr( $name, $chIdx, 1 ) = 'a';
}
$name = reverse $name;
$name = 'a' x ( 1 + length $name ) if $name =~ /^a+$/;
}
return "$name.$ext";
}
I really like how this works, and it took me all day to figure it out. I like how he deals with the case of the triple z. He turns them all into a's with the tr operator, and since they're the only thing that can do that, he tests for that in his if condition, which, if true, uses the x operator to create what I had to have a full-on loop for. Also nice was how to deal with the case of having 'ilo' right in the test condition for the loop, with the tr operator again, and the loop is entered only if nothing transliterates. If something does, you skip the loop and you're done. Also, the double reverse has us working on the right end of things in order to be in proper lexicographic order as it's thought of in english, unlike mine, which was working left to right. Pretty nifty, all in all. |