in reply to Re: •Re: Keeping only the $n newest files/directories in a diretory?
in thread Keeping only the $n newest files/directories in a diretory?

The Schwartzian Transform is a technique to optimize the sorting of complex data structures in Perl.

The simplest case would be to do a single line sort:

my @newest_first = sort { -M $a <=> -M $b } </var/script/proc/*>;

However, to sort, one must compare pairs of items, often comparing the same $a to many different $bs, and vice versa. All those -M checks take a lot of time.

Read a Schwartzian Transform like this starting with the last line.

  • <.../*> is a shorthand call to glob() that returns a list of files.
  • The map above that takes each filename and pairs it to its own -M check results. So you get a list of "foo.blah", timestamp pairs. The square brackets keep each pair in their own little referenced array.
  • The sort line sorts these pairs numerically descending by their second (index 1) element, the timestamps.
  • The map at the top will turn the sorted list of pairs into a list of filenames again, keeping the sorted order.

    --
    [ e d @ h a l l e y . c c ]

    • Comment on Re: Re: &bull;Re: Keeping only the $n newest files/directories in a diretory?
    • Download Code
  • Replies are listed 'Best First'.
    Re: Re: Re: &bull;Re: Keeping only the $n newest files/directories in a diretory?
    by Juerd (Abbot) on Apr 19, 2003 at 13:11 UTC

      All those -M checks take a lot of time.

      On what antique system is that? Many simple filesystem operations are cached by the filesystem. System calls have overhead, but so does creating an array. And unless you have millions of files, you will probably not notice any difference in speed :)

      Juerd
      - http://juerd.nl/
      - spamcollector_perlmonks@juerd.nl (do not use).