in reply to need helping sorting

Let's see... this may do the trick (untested):
opendir(FILES,"$SAVE_DIRECTORY"); @allfiles = sort { (stat $a)[9] <=> (stat $b)[9] } (grep(!/^\.\.?$/,re +addir(FILES))); closedir(FILES); foreach $file (@allfiles) { print "$file " . (stat $file)[9] . "\n"; } exit;
...of course this sorts by Last Modified date, so far as I know there isn't a way to sort by actual file creation date.

Hot Pastrami

Replies are listed 'Best First'.
Re: Re: need helping sorting
by arturo (Vicar) on Mar 15, 2001 at 02:59 UTC

    This will work, but it's not so efficient (you end up redoing the stat calls on each file in that sort). So what you might do is grab the files and their modification times, storing that in a hash (filename => modtime) then sort on modtime, e.g. :

    opendir FILES, $dirname or die "Can't open $dirname:$!\n"; chdir $dirname; #to make sure we're in the right directory! # build the hash, in one line! YES! IF YOU ACT NOW, ALL THIS AND MOR +E # CAN BE YOURS! ... </perl_salesman> my %files = map {$_ => -M $_} grep { !/^\.{1,2}$/ } readdir FILES; closedir FILES; foreach my $file ( sort { $files{$a} <=> $files{$b} } keys %files) { printf("file %s was last modified %.2f days ago", $file, $files{$f +ile}); }

    HTH

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Re: Re: need helping sorting
by anthracite98 (Initiate) on Mar 15, 2001 at 02:38 UTC
    tried it...
    print "Content-type: text/html\n\n"; opendir(FILES,"$SAVE_DIRECTORY"); #@allfiles = sort(grep(!/^\.\.?$/,readdir(FILES))); @allfiles = sort { (stat $a)[9] <=> (stat $b)[9] } (grep(!/^\.\.? +$/,readdir(FILES))); closedir(FILES); foreach$file(@allfiles) { #print "<a href=\"/apply/$file\">$file</a><br>\n"; print "<a href=\"/apply/$file\">$file</a>" . (stat $file)[9] . " +<br>\n"; } exit;
    got this output:
    resume_.1268.1 tm06b.jpg coolurls.jpg r.xls cb-appt.htm test.html982617983 aaa.txt982624016
    which is not sorted by date...also, not sure what the #'s on the last 2 lines are... any more ideas??
      Oops, need to specify the path when using stat():
      opendir(FILES, $SAVE_DIRECTORY); @allfiles = sort { (stat "$SAVE_DIRECTORY/$a")[9] <=> (stat "$SAVE_DIR +ECTORY/$b")[9] } (grep(!/^\.\.?$/,readdir(FILES))); closedir(FILES); foreach $file (@allfiles) { print "$file " . (stat "$SAVE_DIRECTORY/$file")[9] . "\n"; } exit;
      The number at the end is the modify date in Unix epoch format. You may want to translate it to a more readable format, but I can't remember how to do that right now (WAY too much Java this week).

      Update: I think the module to convert the epoch date to a more useable one is Date::Manip, if I recall correctly.

      Hot Pastrami

        You aught to just be able to do a scalar(localtime((stat "$SAVE_DIRECTORY/$file")[9])) to convert it to a fairly nicely formatted string. Date::Manip would be a lot more flexible/powerful, though...

        bbfu
        Seasons don't fear The Reaper.
        Nor do the wind, the sun, and the rain.
        We can be like they are.