Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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:

#!/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; }
Thanks in advance

In reply to Grepping arrays, any better way to do this? by chanakya

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-19 21:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found