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

O'Dear Monks

PLEASE HELP

I have the following script
use strict; open (LST, "c:\\list.lst")||die "$^E : $!\n"; chomp (my @data=<LST>); my $app_sync_dir = 'c$\program files\common files\ms sysadmin\log\Appl +icationSynchronizer'; for (@data) { my $target_dir = "\\\\".$_."\\".$app_sync_dir; print "$target_dir\n"; if (-e $target_dir) { opendir (DIR, $target_dir) || warn "\n$_ : No log file was fou +nd\n"; chomp(my @status = readdir(DIR)); for (@status) { next if (($_ eq '.')||($_ eq '..')); print "$_ : "; my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$m +time,$ctime,$blksize,$blocks) = stat( $target_dir."\\".$_); print "$mtime\n"; } } else { print " : No ApplicationSynchronizer folder was detacted.\n"; + } print "\n************************\n"; } close (LST);
The script obtains the date stamp of the files and displays it.

It works fine, but what I want to do is to select the most recent file. For which I am not sure how to do in simple few steps (I suppose I can but it will be quite a long code).

So what I need is something like (below is all wrong because I don't know how to do it but it looks like something I have seen else where)
sort{$a <=>comp <=> $b} #this is wron but it looks like something I ha +ve seen
Can somebody please enlighten me.

Thanks
Blackadder

Replies are listed 'Best First'.
Re: Sorting out file dates on a remote Win32 PC
by ikegami (Patriarch) on Sep 07, 2004 at 19:15 UTC

    Note 1:
    $a <=> $b compares numerically.
    @a = sort { $a <=> $b } (1, 5, 3);
    $a cmp $b compares alphabetically.
    @a = sort { $a cmp $b } qw(apple orange banana);

    Note 2: readdir's outputs doesn't need to be chomped.

    This will do the trick:

    @status = sort { my $mtime_a = (stat("$target_dir\\$a"))[9]; my $mtime_b = (stat("$target_dir\\$b"))[9]; $mtime_a <=> $mtime_b } @status;

    But it's very expensive because stat can be called multiple times for each file. So here's the workaround (called the Schwartzian Transform):

    @status = ( map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { [ $_, stat("$target_dir\\$_"))[9] ] } @status );
      Brilliant,..it worked - of course - Thanks.
      Blackadder