Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

(tye)Re: find "x" quantity newest files in a dir

by tye (Sage)
on Jun 27, 2001 at 21:25 UTC ( [id://91995]=note: print w/replies, xml ) Need Help??


in reply to find "x" quantity newest files in a dir

If you can have a truely huge number of files, then the best way I know to do this is to use a heap. A heap is a partially sorted binary tree that can be stored in a fixed-length array where inserts are O(log(heap size)). You read through the list of files inserting them into a heap of size 10 which will always hold the 10 newest files so far. If you go to insert the next file and find that it isn't in the top 10, then it doesn't get inserted into the heap so you delete it.

When you are done you have only the 10 newest files remaining in the directory. You can then pull the files out of the heap in sorted order (and you've done a "heap sort"), though your problem doesn't appear to have much use for this last step.

There are a couple of modules on http://search.cpan.org/ that implement heaps in Perl. Unfortunately, I haven't played with them enough to know if they are versatile enough for this type of work, but I'd be happy to hear back if someone else knows.

I mention this not so much because I think it will be a good choice in this specific case. It is just a great way of solving this type of problem if you really need it to scale well (and is one of my personal favorites).

        - tye (but my friends call me "Tye")
  • Comment on (tye)Re: find "x" quantity newest files in a dir

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-04-16 07:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found