__BEGIN__ use strict; use warnings;
die "No file name supplied.\n" unless @ARGV; my $oldest_name = shift @ARGV; my $oldest_age = -C $oldest_name;
foreach (@ARGV) { my $age = -C; ($oldest_name, $oldest_age) = ($_, $age) if ($age > $oldest_age); }
printf "The oldest file is '%s', and it is %.1f days old.\n", $oldest_name, $oldest_age; __END__
This is a modified version of the solution to exercise 11-3 in Learning Perl (which I assume you are reading). The unmodified version uses '-M' not '-C'.
Radhika Sambamurti wrote:
Hi, I have written a small script that is supposed to tell me the oldest file in my directory (as per ctime). I have read the various times the various files were created, into an array called times. I have then sorted this array - @sorted_times. when i do ls -l i get the following:
wxrwxr-x 1 radhika wheel 1028 Mar 24 11:57 chapter11_1.pl -rwxrwxr-x 1 radhika wheel 387 Mar 24 12:37 chapter11_2.pl -rwxrwxr-x 1 radhika wheel 551 Mar 24 21:25 chapter11_3.pl -rwxrwxr-x 1 radhika wheel 281 Mar 15 16:28 file_basename.pl -rw-rw-r-- 1 radhika wheel 236 Mar 21 11:46 filereader.pl -rw-rw-r-- 1 radhika wheel 115 Mar 24 12:04 new.txt -rw-rw-r-- 1 radhika wheel 115 Mar 24 12:00 rad.txt -rwxrwxr-x 1 radhika wheel 648 Mar 15 15:40 rm_file_30days.pl -rwxrwxr-x 1 radhika wheel 554 Mar 15 16:37 rmoldshares.pl
and when I execute the program I get the following:
radhika$ ./chapter11_3.pl * Mon Mar 15 15:55:58 2004 Mon Mar 15 16:28:50 2004 Mon Mar 15 16:37:46 2004 Sun Mar 21 11:46:36 2004 Wed Mar 24 11:57:21 2004 Wed Mar 24 12:00:15 2004 Wed Mar 24 12:04:01 2004 Wed Mar 24 12:37:10 2004 Wed Mar 24 21:26:56 2004
my question is - where did the very first line from my output come from? ie Mon Mar 15 15:55:58. As you can see, ls -l does not show any file created at that time. Even . and .. are not the above time. Is it using sort(@array), that sorts it in some manner that I do not understand?
Thanks, Radhika ------ I've pasted the code for the script below. ---------------------------------------- #! /usr/bin/perl -w use strict; use diagnostics;
my $create_time; my $i=0; my @times; my @sorted_times; my $file;
foreach $file (@ARGV) { my($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks) = stat($file); $create_time = localtime($ctime); $times[$i] = $create_time; $i++; } @sorted_times = sort(@times); my $this_time; foreach $this_time (@sorted_times) { my($day, $mon, $dt, $tm, $yr) = split /\s+/,$this_time; my ($hr, $mn, $sec) = split /:/, $tm; print "$this_time\n";
}
-- Flemming Greve Skovengaard Be it the Devil or be it him a.k.a Greven, TuxPower You can count on just one thing <[EMAIL PROTECTED]> When the time is up, you'll know 4168.08 BogoMIPS Not just one power runs the show
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>