Good day. I have an issue and am new to perl. Here is my issue and what I wish to accomplish. Any help would be greatly appreciated.
I have a csv file which has a filename reference to a specific date. What I want to be able to do is to parse the file, looking for todays date, and then grab the first column entry on the line before it.... Clear as Mud ?
Here is a sample. We'll call this sample.csv
ABC1157,20090606,201003
ABC1158,20090607,201003
ABC1159,20090608,201003
ABC1160,20090609,201003
ABC1161,20090610,201003
ABC1162,20090611,201003
ABC1163,20090612,201003
ABC1164,20090613,201003
ABC1165,20090614,201003
Now the script should be able to determine today's current date in YYYYMMDD format, then open up this file and find that date. So if today is 20090609, and I search the sample.csv file above, I want to grab the ABC1159 entry and save as a variable. Later on with additional processing, I will refer to that entry, then check if that file name exists in the directory.
I've been playing with it a little bit, and saw how some people were using the localtime to get it to print the current date, but when I tried to use that as a search pattern, it was only grabbing part of the date. Snippet below.
my($day, $month, $year, $dexter) = (localtime)[3,4,5];
$month = sprintf '%02d', $month+1;
$day = sprintf '%02d', $day;
$dexter = $year+1900, $month, $day;
open CSVFILE, "sample.csv" or die $!;
while ($line = <CSVFILE>) {
if ($line =~ m/($dexter)/) {
in the if line search patter above, if I put in the date 20090609 it will find it, if I use the $dexter variable, it will only look for 2009 and have multiple finds.
Would reading the entire file into an array be the better step here ?
Bookmarks