| Author |
Help comparing two fields
|
|
| mmittiga17@gmail.com 2006-09-25, 6:57 pm |
| I have a csv file that I want to compare the value in field 22 to a
varialbe
if the value of field 22 is greater than the variable I want to skip
to the next line. if it is not => I want to print the line.
while ($line = <tempxxxI> ) {
$line =~ s/'//g;
$csv = Text::CSV->new();
$csv->parse($line) && $csv->fields();
@fields = $csv->fields();
next if $fields[21] => $Date7;
print OUT $line;
}
Problem I am having is dates are in the mm/dd/yyyy format 09/25/2006.
How do you compare values like this?
Thanks
| |
| Paul Lalli 2006-09-25, 6:57 pm |
| mmittig...@gmail.com wrote:
> I have a csv file that I want to compare the value in field 22 to a
> varialbe
>
> if the value of field 22 is greater than the variable I want to skip
> to the next line. if it is not => I want to print the line.
>
> while ($line = <tempxxxI> ) {
> $line =~ s/'//g;
> $csv = Text::CSV->new();
> $csv->parse($line) && $csv->fields();
> @fields = $csv->fields();
> next if $fields[21] => $Date7;
Please post *real* code.
> print OUT $line;
> }
>
> Problem I am having is dates are in the mm/dd/yyyy format 09/25/2006.
> How do you compare values like this?
Please check the built-in Perl FAQ *before* posting:
perldoc -q compare
Found in /opt2/Perl5_8_4/lib/perl5/5.8.4/pod/perlfaq4.pod
How can I compare two dates and find the difference?
Paul Lalli
>
> Thanks
| |
| mmittiga17@gmail.com 2006-09-25, 6:57 pm |
| perldoc was of know help. it does not explain or show how to compare
fields that contain "/".
Paul Lalli wrote:[color=darkred]
> mmittig...@gmail.com wrote:
>
> Please post *real* code.
>
>
> Please check the built-in Perl FAQ *before* posting:
> perldoc -q compare
> Found in /opt2/Perl5_8_4/lib/perl5/5.8.4/pod/perlfaq4.pod
> How can I compare two dates and find the difference?
>
> Paul Lalli
>
| |
| Paul Lalli 2006-09-25, 6:57 pm |
| [PLEASE DO NOT TOP-POST!!]
mmittig...@gmail.com wrote:
> Paul Lalli wrote:
[color=darkred]
> perldoc was of know help. it does not explain or show how to compare
> fields that contain "/".
>
The hell it wasn't. Did you *read* it, or did you just scan it for the
exact answer to your question? It tells you exactly how to compare
dates. You cannot compare just the string values. You have to parse
them out to find the month, date, and year. Then you have to use those
values to find an epoch-time representation of that date.
Paul Lalli
| |
| usenet@DavidFilmer.com 2006-09-25, 6:57 pm |
| mmittig...@gmail.com wrote:
[please don't toppost]
> perldoc was of [no] help. it does not explain or show how to compare
> fields that contain "/".
Did you actually READ it? Maybe it doesn't hold your hand and describe
the exact format you're using, but you ought to be able to get a clue
about how to proceed for ANY given date format:
[color=darkred]
Hmmmm... I'd check that out if I were you...
--
David Filmer (http://DavidFilmer.com)
| |
|
| As they say - in perl there is more than one way to do something.
If all you need to do is to compare the dates and find which is greater
without worrying about how many days are actually between them - then
here is a trick - just turn the mm/dd/yyyy date into a number made up
of yyyymmdd: This shows how it works
#!/usr/bin/perl
use strict;
my $entry;
my $prevday="0";
my @dates =("09/25/2006","10/01/2006","07/09/2006","09/27/2001");
foreach $entry(@dates){
my @day =split(/\//,$entry);
my $yyyymmdd ="$day[2]$day[0]$day[1]";
if ($prevday < $yyyymmdd ) {
print "new date $yyyymmdd is later than previous day $prevday
\n";
}
$prevday = $yyyymmdd;
}
|
|
|
|