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

Sequential names

by turnstep (Parson)
on Mar 23, 2001 at 19:58 UTC ( [id://66648]=CUFP: print w/replies, xml ) Need Help??

Just a little snippet to return sequential file names of the form aaaa, aaab, aaac, etc. When a character gets to 'z', it resets again to 'a' and increments the next character to the left by one. If it runs out of names, it switches to the POSIX tmpnam() function as a last resort. You can use any number of characters, but should not need too many: X characters will generate 26^X combinations before resorting to tmpnam(). The second argument is the direction, anything but a minus number means ascending. If you don't care about clobbering existing files, just throw in a third argument. Sample usage:

my $filename = "xyz"; for (1..10) { print qq[Filename is "$filename"\n]; $filename = &newname($filename,1); }

I recommended that you keep track of the names generated unless you are overwriting so that you can be aware of any "holes" in the sequence.

Update: Made it ascending and descending based on tye's challenge^H^H^H^H^H^H^H^H^Hcomment, and that fact that $filename-- fails miserably to act like $filename++ does. :)

sub newname() { my $filename = shift || "aaaa"; my $sign = shift || 1; my $overwrite = shift; { my $x = 1; 1 while substr($filename,-$x++,1) eq ($sign<1 ? "a":"z"); if (--$x > length $filename) { $filename = tmpnam(); } else { substr($filename,-$x--,1)=chr(ord(substr($filename,-$x,1))+$sign +); substr($filename,-$x--,1)=($sign<1 ? "z":"a") while $x; } redo if !$overwrite and -e $filename; return $filename; } }

Replies are listed 'Best First'.
(tye)Re: Sequential names
by tye (Sage) on Mar 23, 2001 at 20:06 UTC

    Most of this code can be replaced by $filename++ since $filename= "xyz"; $filename++; leaves $filename set to "xza".

            - tye (but my friends call me "Tye")
(tye)Re2: Sequential names
by tye (Sage) on Mar 23, 2001 at 23:39 UTC

    Most of this new code can be replaced by:

    sub compl { $_[0] =~ tr/a-z/zyxwvutsrqponmlkjihgfedcba/; \$_[0] } compl( ++${compl($filename)} );
    q-:

            - tye (but my friends call me "Tye")
Re (tilly) 1: Sequential names
by tilly (Archbishop) on Mar 23, 2001 at 21:23 UTC
    I am trying to imagine a use for this that wouldn't be better served with File::Temp or simply numbered files and I am failing.

    Could you explain what this might be useful for? Thanks.

    BTW if you are really bored, you could use Math::Fleximal as the basis for another implementation that gives great flexibility in the character set and allows incrementing and decrementing...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-25 17:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found