in reply to Re: need helping sorting
in thread need helping sorting

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??

Replies are listed 'Best First'.
Re: Re: Re: need helping sorting
by Hot Pastrami (Monk) on Mar 15, 2001 at 02:47 UTC
    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.