mito has asked for the wisdom of the Perl Monks concerning the following question:

I have this code
opendir(IMD, $directoryOfImages) or die "cannot open $directoryOfImage +s: $!"; @allPictures=<IMD>; @allPictures= grep { $_ ne '.' and $_ ne '..' } readdir IMD; { @allPictures = sort(@allPictures); } closedir IMD;

It reads the files by name. I'd like to know how to read all the files by creation date I am working on tripod.So don't recommend me modules please.

Thanks in advance

20031130 Edit by Corion: Added CODE tags, formatting

Replies are listed 'Best First'.
Re: sort by date
by Abigail-II (Bishop) on Nov 30, 2003 at 17:23 UTC
    Perhaps something like (untested):
    opendir my $dir => $directoryOfImage or die; my @allPictures = map {substr $_ => 10} sort map {sprint "%010d%s" => (stat) [9], $_} grep {$_ ne "." && $_ ne ".."} readdir $dir; closedir $dir;

    So don't recommend me modules please.
    Why not?

    Abigail

Re: sort by date
by Limbic~Region (Chancellor) on Nov 30, 2003 at 17:27 UTC
•Re: sort by date
by merlyn (Sage) on Nov 30, 2003 at 17:57 UTC
    by creation date
    Well, no matter how hard you try, you can't get creation date on Unix. Unix doesn't maintain a "creation date", which the founders of Unix argued doesn't actually make sense (and I agree, sometimes).

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Yes... that's true on UNIX but not on Win32...

      From perldoc perlport:
      stat ctime not supported on UFS (Mac OS X). ctime is creation time instead of inode change time (Win32).
      So, if the author is only trying to write code on a Win32 platform, with no cares or worries wrt portable code, then (s)he *can* get the creation time.

      But don't do that.

      You almost certainly want to know which file has newer or older *content* than the others, i.e. which has been modified most (or least) recently.

      :)
Re: sort by date
by forrest (Beadle) on Nov 30, 2003 at 17:27 UTC
    If you
    use File::stat; # don't worry, it's part of standard perl
    then you can
    @allPictures = sort {stat($a)->mtime <=> stat($b)->mtime} @allPictures;
    That should do it.
      I would rather do an orcish manueuver instead. ;-)

      use File::stat; ... my %m; @allPictures = sort { ($m{$a} ||= stat($a)->mtime) <=> ($m{$b} ||= stat($b)->mtime) } @allPictures;
      or simpler version -
      my %m; @allPictures = sort { ($m{$a} ||= -M $a) <=> ($m{$b} ||= -M $b) } @allPictures;
Re: sort by date
by jeffa (Bishop) on Nov 30, 2003 at 17:21 UTC

      That's fine, but not much use if you don't have shell access.

      (Granted you could install a pure perl module by creating the directories by hand, but satisfying dependencies would get painful after a while...)

      Steve
      ---
      steve.org.uk
Re: sort by date
by b10m (Vicar) on Nov 30, 2003 at 16:41 UTC
Re: sort by date
by Cody Pendant (Prior) on Nov 30, 2003 at 19:56 UTC
    @allPictures=<IMD>;
    What was the point of this line?


    ($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss') =~y~b-v~a-z~s; print
      People often try that. And then ask for perl to change to make it work. The usual answers are:
    • you can use tie if you want <> to automatically become readdir
    • since perl can have both a file and dir open with handle IMD, this becomes ambiguous