Shannon Murdoch wrote:
>
> Hi all,
Hello,
> I have a list of 40 or so files that need to be sorted into an array by
> article date (found in first line of each file in the form DD-MM-YYYY).
> I can't figure out how to go about it successfully... Can anyone help me out
> please??
>
> Thanks in advance!
>
> Current method is as follows:
use warnings;
use strict;
> use Time::Local;
>
> @list = glob("*.txt"); ## makes an array of files ending in .txt eg 03.txt
>
> foreach $file (@list) { ##find dates...
> open(FEEDBACK,"$file");
You should _always_ check the return value from open.
open FEEDBACK, $file or die "Cannot open $file: $!";
> while(<FEEDBACK>){
If you only need the first line there is no reason to use a while loop.
> chomp($_);
> @feedbackdate = split(/-/,$_); ## eg @feedbackdate = ('07','02','1982')
> $epochseconds = timelocal(1, 1, 1, $feedbackdate[0], $feedbackdate[1]-1,
> $feedbackdate[2]-1900);
> @feedbackdate = (); ##empty the hash for next loop.
> $filelisthash{$file} = $epochseconds;
> last;
> }
> close(FEEDBACK);
> } #end loop
>
> @list = (); ##prepare the list array
>
> foreach $file_num (sort { $a cmp $b } keys %filelisthash) {
> push(@list,$file_num);
> }
>
> print @list;
#!/usr/bin/perl
use warnings;
use strict;
use Time::Local;
my @filelist;
for my $file ( glob '*.txt' ) {
open FEEDBACK, $file or die "Cannot open $file: $!";
my $line = <FEEDBACK>;
close FEEDBACK;
my ( $day, $mon, $year ) = $line =~ /(\d+)/g;
my $seconds = timelocal( 0, 0, 0, $day, $mon - 1, $year - 1900 )
push @filelist, [ $seconds, $file ];
}
for my $file ( sort { $a->[0] <=> $b->[0] } @filelist ) {
print "$file->[1]\n";
}
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]