As your data looks to be time sequential something easy like this would probably do you fine. I have given a simple way to do it with two hashes or a more perlish way with a hash of arrays. If your data lines are not ordered by time then a check to compare two time stamps will be needed for each line to decide if to update first or last time.
#!/usr/opt/perl5/bin/perl
use strict;
use warnings;
use Data::Dumper;
my %earliest;
my %latest;
my %hash_of_arrays;
my $line;
while ($line = <DATA>) {
chomp $line; # remove return
my ($key, $date) = split /\s+/, $line, 2; # split on space into ma
+x 2 parts
next unless $date; # ignore blank or malfor
+med lines
unless (defined $earliest{$key}) {
$earliest{$key} = $date;
}
$latest{$key} = $date;
# or with a hash of arrays
$hash_of_arrays{$key} or $hash_of_arrays{$key}[0]=$date;
$hash_of_arrays{$key}[1]=$date;
}
print Data::Dumper->Dump([\%earliest], ['%earliest']) ;
print Data::Dumper->Dump([\%latest], ['%latest']);
print Data::Dumper->Dump([\%hash_of_arrays], ['%hash_of_arrays']);
print "first time for KEY1 was: $hash_of_arrays{KEY1}[0]\n";
print "last time for KEY1 was: $hash_of_arrays{KEY1}[1]\n";
__DATA__
KEY1 07/25/05 09:04:36
KEY2 07/25/05 09:04:36
KEY2 07/25/05 09:04:37
KEY2 07/25/05 09:05:49
KEY2 07/25/05 09:05:50
KEY3 07/25/05 09:05:50
KEY1 07/25/05 09:06:36
# output
=>./first
$%earliest = {
'KEY2' => '07/25/05 09:04:36',
'KEY1' => '07/25/05 09:04:36',
'KEY3' => '07/25/05 09:05:50'
};
$%latest = {
'KEY2' => '07/25/05 09:05:50',
'KEY1' => '07/25/05 09:06:36',
'KEY3' => '07/25/05 09:05:50'
};
$%hash_of_arrays = {
'KEY2' => [
'07/25/05 09:04:36',
'07/25/05 09:05:50'
],
'KEY1' => [
'07/25/05 09:04:36',
'07/25/05 09:06:36'
],
'KEY3' => [
'07/25/05 09:05:50',
'07/25/05 09:05:50'
]
};
first time for KEY1 was: 07/25/05 09:04:36
last time for KEY1 was: 07/25/05 09:06:36
Cheers, R.
Pereant, qui ante nos nostra dixerunt!
|