in reply to Re: Listing old files
in thread Listing old files

Yes what i need is to list oldest file in each sub directory and i tried as per your suggestion using hash but this is listing for all the files instead of oldest file
$data{$File::Find::name} = scalar(localtime($age));

Replies are listed 'Best First'.
Re^3: Listing old files
by Ratazong (Monsignor) on May 09, 2011 at 11:04 UTC

    not quite:

    The idea is to create one age and one name for each subdirectory. This grouping can be done by a hash. However you should use the "grouping-criteria" as hash-key. In your case it is $File::Find::dir. The rest of your logic can remain unchanged.

    use strict; use warnings; use File::Find; @ARGV = ('.') unless @ARGV; my %age; my %name; sub oldest { return if ((defined $age{$File::Find::dir}) && ($age{$File::Find::di +r} < (stat($_))[9])); $age{$File::Find::dir} = (stat(_))[9]; $name{$File::Find::dir} = $File::Find::name; } find(\&oldest, @ARGV); foreach my $n (keys(%age)) { print $name{$n}." " . scalar(localtime($age{$n})) . "\n"; }

    btw.: I'm unsure whether this is a homework-question or not ... if it is be sure that you totally understand the solution. I have seen many situations where a student couldn't explain some code supposedly written by himself. You don't want that happen to you! ;-)

    Rata