Aldebaran has asked for the wisdom of the Perl Monks concerning the following question:
Hello Monks,
I wanted to develop a capability to generate new, unique filenames, so I thought that I would write a routine to do so. The idea is that it strips the input to its basename, then finds the next value in lexicographic order that a given alphabet can generate. Finally, it pastes the extension back on and sends it back. I took the prudent step of eliminating the letters that can easily be confused with numbers in this list. What I have seems to work, so what I'm looking for are improvements to the style, form, and robustness. This isn't looking very perlish yet. What follows is the caller, the routine, and stdout.
my @filenames = qw/a.png dpq.jpg zx.png ott.jpg zzz.tiff oi.png/; foreach (@filenames) { my $next = next_file($_); say "next is $next"; }
sub next_file { use strict; use warnings; use 5.010; my ($previous) = shift; my @alphabet = qw/a b c d e f g h j k m n p q r s t u v w x y z/; $previous =~ /(.*)\.(.*)/; my $word = $1; my $ext = $2; say "word is $word"; my @letters = split( //, $word ); my $z = 0; # main control outer loop...all indexes begin with 0 foreach my $dummy ( 0 .. $#letters ) { foreach my $index ( 0 .. $#alphabet ) { if ( $alphabet[$index] eq $letters[$dummy] ) { my $highest = $#alphabet; if ( $index < $highest ) { $letters[$dummy] = $alphabet[ $index + 1 ]; my $string = join( '', @letters, ".$ext" ); return $string; } else { say "it was a $alphabet[-1]"; $z = $z + 1; } } } } if ( $z eq ( $#letters + 1 ) ) { say "number of zees was $z"; my $newindex = $#letters + 1; my @newword; foreach ( 0 .. $newindex ) { $newword[$_] = $alphabet[0]; } my $newstring = join( '', @newword, ".$ext" ); return $newstring; } else { say "snakeeyes: you hit nothing with $word"; my $string3 = join( "", $alphabet[0], ".$ext" ); return $string3; } }
word is a next is b.png word is dpq next is epq.jpg word is zx it was a z next is zy.png word is ott next is out.jpg word is zzz it was a z it was a z it was a z number of zees was 3 next is aaaa.tiff word is oi snakeeyes: you hit nothing with oi next is a.png
Thanks for your comment.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: generating unique filenames
by GrandFather (Saint) on Oct 03, 2014 at 03:47 UTC | |
by Aldebaran (Curate) on Oct 04, 2014 at 00:46 UTC | |
|
Re: generating unique filenames
by Athanasius (Archbishop) on Oct 03, 2014 at 02:48 UTC | |
by Aldebaran (Curate) on Oct 05, 2014 at 19:38 UTC | |
by Athanasius (Archbishop) on Oct 06, 2014 at 02:58 UTC | |
|
Re: generating unique filenames (next letter in list)
by Anonymous Monk on Oct 03, 2014 at 02:30 UTC | |
by Anonymous Monk on Oct 03, 2014 at 03:06 UTC | |
|
Re: generating unique filenames
by karlgoethebier (Abbot) on Oct 03, 2014 at 11:21 UTC | |
|
Re: generating unique filenames
by McA (Priest) on Oct 04, 2014 at 00:30 UTC | |
|
Re: generating unique filenames
by Anonymous Monk on Oct 04, 2014 at 01:00 UTC |