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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.