Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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")

In reply to (tye)Re: find "x" quantity newest files in a dir by tye
in thread find "x" quantity newest files in a dir by braintoast

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-04-19 14:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found