Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Cleaning up by age

by Jazz (Curate)
on Nov 13, 2001 at 00:31 UTC ( [id://124907]=note: print w/replies, xml ) Need Help??


in reply to Cleaning up by age

Here's an easy way to glob, sort, and propagate an array with the 10 oldest files in a directory...
my $debug = 1; my @oldest_10_files = ( map { $_->[1] } sort { $b->[0] <=> $a->[0] } map { [ -M $_, $_ ] } glob( "/path/to/directory/*" ) )[0..9]; unlink @oldest_10_files unless $debug; print "The following files have been removed\n\n", join "\n", @oldest_ +10_files;
Hope this helps.

Update: The above code utilizes the Schwartzian Transform. Thanks, Jonathan for pointing out the oversight.

Replies are listed 'Best First'.
Re: Re: Cleaning up by age
by Jonathan (Curate) on Nov 15, 2001 at 18:21 UTC

      If you are going to use a Schwartzian Transform please give credit where due.

      Eh? The ST's an idiom, not some sort of proprietary construct. It's not as if there's some great dispute over whose leap of hackerly brilliance it was -- the credit is right in the name! When someone sees map/sort/map they'll immediately think "Oh, a Schwartzian Transform". Immediate credit to Merlyn.

      That said, pointing out that a ST was used is probably a Good Thing -- that way, a novice (or maintenance programmer :-) confronted with the code has some recourse beyond "what the hell's up with all these maps?" The ST is pointed out, so the unfamiliar reader can go off and read up on STs... and learn something in the process.

      --
      :wq

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2024-03-28 19:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found