Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Do I need a list, scalar, array, or hash???

Status
Not open for further replies.

networkwoes

Technical User
Aug 17, 2001
13
0
0
US
Ok, here is the deal. I have what I think should be a VERY easy task, accompanied by a brain freeze. What i need to do is to exctract the highest and lowest date from a file and assign them to two variables.

The file I am working with has three columns and the second and third columns are dates. I want to somehow join the second and third column together and pull out the newest and oldest dates and assign to $lowdate and $highdate.

Here is an example of the file i am extracting from:
1,19920101,20010115
1,19930101,20020115
1,19940101,20010115
1,19950101,20060115
1,19960101,20010115
1,19970101,20110115
1,19980101,20010115
1,19990101,20010115
1,20000101,20010115
1,20010101,20310115
1,20020101,19710115

any help would be greatly appreciated!

 
my ($lowdate,$highdate, @tmp);
open (FILE, &quot;<$file&quot;);
while (<FILE>){
@tmp = split /,/, $_;
$lowdate = $_ if ($tmp[1] <= $lowdate);
$highdate = $_ if ($tmp[1] >= $highdate);
}
close FILE;
print &quot;H: $highdate L: $lowdate&quot;;
 
There's a problem here: $lowdate will never get assigned,
since none of the dates in the file will ever be less than
or equal to $lowdate's initial value, which is undefined.

Networkwoes, what do you mean when you say you want to
&quot;somehow join together the second and third columns&quot; in
the file? What do you figure your final output should be,
two entire records (one containing the $lowdate and one the
$highdate, which is what LightElf is shooting for in the
code above), or just two simple variables?
 
Without waiting for your reply about the output, here are
2 solutions that assume you just want 2 simple variables, one containing the earliest date in the file, the other the latest date. Though the second solution is much shorter, I kind of feel like it's cheating since I'm using a sort to put the numbers in order. I prefer the first one.
Both return the same answers on your example data. (Note that what these give for $highval is different from LightElf's output. I just noticed that LightElf's code
never even looks at the second date in each record, only the first one.)

#First solution:

my ($lowdate, $highdate);
my ($d1, $d2);

while (chomp($_=<>)) {
($d1, $d2) = (split ',')[1,2];

if ($. == 1) {
# initialize $lowdate and $highdate
$lowdate = min($d1, $d2);
$highdate = max($d1, $d2);
} else {
$lowdate = min(min($d1, $d2), $lowdate);
$highdate = max(max($d1, $d2), $highdate);
}
}
print &quot;\$lowdate: $lowdate\n&quot;;
print &quot;\$highdate: $highdate\n&quot;;

sub min($$) {
my ($x, $y) = @_;
$x < $y? $x: $y;
}

sub max($$) {
my ($x, $y) = @_;
$x > $y? $x: $y;
}


#Second solution:

my @ary;

push @ary, (split ',')[1,2] while chomp($_=<>);
my ($lowdate, $highdate) = (sort @ary)[0,-1];

print &quot;\$lowdate: $lowdate\n&quot;;
print &quot;\$highdate: $highdate\n&quot;;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top