Re: How can I sort files by timestamp without slurping?



icarus wrote:
>
hi are you doing everybody...

How can I sort files by timestamp without slurping?

the idea is to look into a directory, pick up the oldest first and so
on until the 'youngest' one.
file100..all the way to file1.

I found this solution somewhere,

my @sorted = map { $_->[0] }
sort { $b->[1] <=> $a->[1] }
map { [ $_, -M $_ ] }
@files;

By the looks of it, it does slurp the all the files from the source
directory into an array and does the sorting there.

I'm thinking on incorporating a timestamp sorting mechanism without
slurping.

Since above solution looks like sorcery of some kind to me,
How can I modify it to fit below? or if you have any other ideas
please let me know. Thanks in advance.

MOVE_FILES:
while (defined (my $file = readdir (SOURCEDIR))){

#skip . and .. files
next MOVE_FILES if $file =~ m#^\.\.?$#;

#the sorting should it take place here I guess.


#move files
move ($file, $target_path) or die $!;
}

By 'slurping' do you mean reading all of the directory at once? Because
that cannot be avoided as you need a list of files for there to be
anything to sort. If you mean reading all of the individual files tho,
then that's fine as there's no need to read the file at all.

The sort you show has been optimised for speed. In all probability a
standard sort will do fine and you would end up with something like
the code below.

HTH,

Rob



use strict;
use warnings;

my $dir = 'mydir';

opendir my $dh, "$dir\\" or die $!;

my @files = map "$dir\\$_", readdir $dh;

my @sorted = sort { -M $b <=> -M $a } @files;

.



Relevant Pages

  • Re: A Fast sorting algorithm for almost sorted data
    ... far my compressor has potential but is nowhere near ready. ... It does however make heavy use of sorting. ... which I am currently calling Run sort. ... entire selected run can be added to the sorted output array. ...
    (comp.compression)
  • Re: Solution for sorting an array alpha-numerically
    ... strings up into groups and sorting the groups seperately, ... > so that numeric and alphabetic data sort as seperate groups. ... To the same project as the web page, add the class AlphaNumCompare() ...
    (microsoft.public.dotnet.general)
  • Re: how fast can I sort on mainframe (using DFSORT)?
    ... Since I joined the team as the performance lead a couple years ago, ... Frank now defers these types of questions to me. ... I have been out of the sorting business for a while, ... Writing to sort work files should not be the problem, ...
    (bit.listserv.ibm-main)
  • How can I sort files by timestamp without slurping?
    ... How can I sort files by timestamp without slurping? ... I'm thinking on incorporating a timestamp sorting mechanism without ...
    (perl.beginners)
  • Re: When random isnt random
    ... >> (and, if there is not one already, a Sorting Unit). ... TList has a Sort method. ... Try it with a TList and in the compare function ... There seems to be, sometimes, a requirement for a Shuffle that leaves ...
    (borland.public.delphi.language.objectpascal)