chanakya has asked for the wisdom of the Perl Monks concerning the following question:
Dear Esteemed monks,
I'm working on a task which is as below:
* Generate a list of dates from a given start date to a given end date
* Get a list of files for each of the item(these are directories) in the csv file
* Once I have a the two arrays, i.e one array consisting of a list of dates
and the second array with the list of files for a directory,I have to check for the existence of each of the dates from the first array in the second array.
* The following are the structures of the dates array and files array contains the filenames as
@datelist = ("20030901", "20061017", "20050406", "20070101", "20080202"); @fileslist = ("DIR22.20060816", "DIR22.20050919", "DIR22.20061017", "DIR22.20060516", "DIR22.20050406");
I am using grep to check the existence of dates from the first array in the second array.
Below is the code I'm using, please let me know whether this approach is correct or is there any other better way:
I'm working on a task which is as below:
* Generate a list of dates from a given start date to a given end date
* Get a list of files for each of the item(these are directories) in the csv file
* Once I have a the two arrays, i.e one array consisting of a list of dates
and the second array with the list of files for a directory,I have to check for the existence of each of the dates from the first array in the second array.
* The following are the structures of the dates array and files array contains the filenames as
@datelist = ("20030901", "20061017", "20050406", "20070101", "20080202"); @fileslist = ("DIR22.20060816", "DIR22.20050919", "DIR22.20061017", "DIR22.20060516", "DIR22.20050406");
I am using grep to check the existence of dates from the first array in the second array.
Below is the code I'm using, please let me know whether this approach is correct or is there any other better way:
Thanks in advance#!/usr/bi/perl $driver = "./dirlist.csv"; open DRV , $driver || die "Cannot open $driver: $!"; while( <DRV> ){ chomp; my ($dirname) = shift; checkExistingDates($dirname); } close DRV; sub checkExistingDates{ my $dirname = shift; my @datelist = ("20030901", "20061017", "20050406", "20070101", "2 +0080202"); #the file list should be read from the $dirname, for testing using + hard values my @fileslist = ("DIR22.20060816", "DIR22.20050919", "DIR22.200610 +17", "DIR22.20060516", "DIR22.20050406"); my @matched; foreach my $date (@datelist}){ @matched = grep{$date} @fileslist; } print Dumper @matched; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Grepping arrays, any better way to do this?
by bart (Canon) on Feb 15, 2007 at 11:15 UTC | |
Re: Grepping arrays, any better way to do this?
by izut (Chaplain) on Feb 15, 2007 at 11:17 UTC | |
Re: Grepping arrays, any better way to do this?
by akho (Hermit) on Feb 15, 2007 at 11:44 UTC | |
Re: Grepping arrays, any better way to do this?
by chanakya (Friar) on Feb 15, 2007 at 14:05 UTC | |
by 5mi11er (Deacon) on Feb 16, 2007 at 23:00 UTC |
Back to
Seekers of Perl Wisdom