On Fri, Nov 21, 2008 at 16:38, Richard Lee <[EMAIL PROTECTED]> wrote:
> Richard Lee wrote:
>>
>> Richard Lee wrote:
>>>
>>> stion on this.. suppose there is 3 files in temp directory
>>>
>>> /tmp/yahoo1
>>> /tmp/yahoo2
>>> /tmp/yahoo3
>>>
>>> and I wanted to take the last file that was created.. would this work?
>>>
>>> my $filename = shift;
>>>
>>> my @file_1 = </tmp/$filename*>;
>>> my $file_1 = $file_1[-1];
>>> push @files, $file_1;
>>
>> i am not 100% sure if this logic is correct or not.
>>
>> my %big_hash;
>> for (@file_1) {
>>    $big_hash{$_} = (stat($_))[9];
>> }
>>
>> my @keys = sort { $big_hash{$a} <=> $big_hash{b} } keys %big_hash;
>> my $file_1 = $keys[-1];
>> push @files, $file_1;
>>
> I am missing a $ in $big_hash{b}.. and i see that this method works.. but is
> there easier and better(correct) way to do this ???

If the number of files is fewer than a two hundred this is fine, but
if it is more then you really want to use a min routine rather than
sort to find the oldest file:

#FIXME: if two files are the oldest, we can't predict which one will be deleted

my $oldest_name;
my $oldest_date = 0;
for my $file (</tmp/$filename*>) {
        if ($oldest_date >= (stat $file)[8]) {
                # _ is used here to get the cached info
                # from the last stat call
                $oldest_date = (stat _)[8]; last stat call
                $oldest_name = $file;
        }
}

die "no files found" unless defined $oldest_name;

print "the oldest file is $oldest_name\n";

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to