|
Next: swift MT940 files
From: clearguy02 on 7 Feb 2005 16:59 Hi all, Below is the scenario. I have a file that has two entrees (a name and a date) seperated by a tab. I need to get only all those names in the file if the date on the each line is greater than a certain date, 12-31-2004. Script ####################### $current = "31-Dec-04"; foreach (<DATA>) { ($f, $l) = /(\w+)\t+(\w+)/; print $f if ($l > $current); } __DATA__ 5.5.25.50 01-Feb-05 TEST1 22-Jul-04 BSTOP 03-Sep-02 DB00004639 21-Jan-05 DB00004693 25-Jan-05 CDM_3.1.0 27-May-03 ############################ I think I am failing on comparing stuff. How can I rectify the above code? Thanks, Rider.
From: Eric Schwartz on 7 Feb 2005 17:08 clearguy02(a)yahoo.com writes: > I have a file that has two entrees (a name and a date) seperated by a > tab. I need to get only all those names in the file if the date on the > each line is greater than a certain date, 12-31-2004. You should probably get in the habit of searching CPAN before posting here-- that's not an insult, merely an observation that a lot of the time, you'll find the answer there first, and save time. In this case, you'd have found Date::Manip and Date::Calc, both of which will do what you want, more or less. -=Eric -- Come to think of it, there are already a million monkeys on a million typewriters, and Usenet is NOTHING like Shakespeare. -- Blair Houghton.
From: A. Sinan Unur on 7 Feb 2005 17:41 clearguy02(a)yahoo.com wrote in news:1107813544.944788.294170 @c13g2000cwb.googlegroups.com: > I have a file that has two entrees (a name and a date) seperated by a Just FYI: ITYM "entries". > tab. I need to get only all those names in the file if the date on the > each line is greater than a certain date, 12-31-2004. > > Script > ####################### There is usually no need for this kind of separator. If you put a shebang line in your scripts, that serves nicely to set the script apart from the rest of the post, especially if you end your scripts with __END__. use strict; use warnings; missing. Please read the posting guidelines for this group. > $current = "31-Dec-04"; my $current = "20041231"; for reasons that will become clear later. > foreach (<DATA>) { This first reads the whole file. Better to use while (<DATA>) { > ($f, $l) = /(\w+)\t+(\w+)/; > print $f if ($l > $current); So, you have two strings and you compare them using the numeric operator '>'. Indeed, if you had turned on warnings, perl would have told you: Argument "31-Dec-04" isn't numeric in numeric gt (>) at a.pl line 9, <DATA> line 7. > I think I am failing on comparing stuff. How can I rectify the above > code? You could use a module for comparing dates. See CPAN if you want to go that route. On the other hand, you could also just convert your dates to the YYYYMMDD format and use string comparison: #! /usr/bin/perl use strict; use warnings; my $current = '20041231'; my %months = ( Jan => '01', Feb => '02', Mar => '03', Apr => '04', May => '05', Jun => '06', Jul => '07', Aug => '08', Sep => '09', Oct => '10', Nov => '11', Dec => '12', ); while(<DATA>) { chomp; last unless $_; my ($file, $date) = split /\s+/; my ($day, $month, $year) = split '-', $date; # You'll need to figure out how far back the dates go # and adjust the if statement below accordingly if(0 + $year <= 50) { $year += 2000; } else { $year += 1900; } $month = $months{$month}; print "$file\n" if $current le "$year$month$day"; } __DATA__ 5.5.25.50 01-Feb-05 TEST1 22-Jul-04 BSTOP 03-Sep-02 DB00004639 21-Jan-05 DB00004693 25-Jan-05 CDM_3.1.0 27-May-03 __END__
From: Gunnar Hjalmarsson on 7 Feb 2005 18:07 clearguy02(a)yahoo.com wrote: > I have a file that has two entrees (a name and a date) seperated by a > tab. I need to get only all those names in the file if the date on the > each line is greater than a certain date, 12-31-2004. > > Script > ####################### > $current = "31-Dec-04"; > foreach (<DATA>) { > ($f, $l) = /(\w+)\t+(\w+)/; > print $f if ($l > $current); > } > __DATA__ > 5.5.25.50 01-Feb-05 > TEST1 22-Jul-04 > BSTOP 03-Sep-02 > DB00004639 21-Jan-05 > DB00004693 25-Jan-05 > CDM_3.1.0 27-May-03 > ############################ > > I think I am failing on comparing stuff. Obviously. warning: Argument "31Dec04" isn't numeric in numeric gt You need to make things comparable in order to compare them... > How can I rectify the above code? You can: 1) enable strictures 2) enable warnings 3) declare the variables you are using 4) use an appropriate module - I would suggest Date::Parse 5) refrain from unnecessarily loading the whole file into memory 6) study "perldoc perlre" to find out that \w does not match what you think it matches use strict; use warnings; use Date::Parse; my $current = str2time('31-Dec-04'); while (<DATA>) { my ($f, $l) = /(.+)\t(.+)/; print "$f\n" if str2time($l) > $current; } -- Gunnar Hjalmarsson Email: http://www.gunnar.cc/cgi-bin/contact.pl
From: mothra on 8 Feb 2005 09:32 <clearguy02(a)yahoo.com> wrote in message news:1107813544.944788.294170(a)c13g2000cwb.googlegroups.com... > Hi all, > > Below is the scenario. > > I have a file that has two entrees (a name and a date) seperated by a > tab. I need to get only all those names in the file if the date on the > each line is greater than a certain date, 12-31-2004. > Something like this might work. It uses DateTime use strict; use warnings; use DateTime; use DateTime::Format::Strptime; #target date my $dt1 = DateTime->new( year => 2004, month => 12, day => 31, ); my $Strp = new DateTime::Format::Strptime( pattern => '%d-%b-%y', ); while (<DATA>) { chomp; my ( $file, $date ) = split /\s+/; my $dt2 = $Strp->parse_datetime($date); if ( $dt1 > $dt2 ) { print "Date from file is before\n"; } if ( $dt1 == $dt2 ) { print "Dates are the same\n"; } if ( $dt1 < $dt2 ) { print "Date from file is after\n"; } } __DATA__ 5.5.25.50 01-Feb-05 TEST1 22-Jul-04 BSTOP 03-Sep-02 DB00004639 21-Jan-05 DB00004693 25-Jan-05 CDM_3.1.0 27-May-03 I hope this helps Mothra
|
Pages: 1 Next: swift MT940 files |