|
Posted by A. Sinan Unur on April 30, 2008, 6:37 pm
Please log in for more thread options
6ef4dfdb6ddf@w7g2000hsa.googlegroups.com:
> Well I am new, and still trying to learn perl...while at work on 10
> different things....sheesh is there ever enough time to learn
> something..
Still, not much of an excuse not to have tried anything. Please read the
posting guidelines for this group before you post again.
...
<Data snipped here for brevity>
...
> for instance
>
> 155073040~06/04/1998
> 155073040~04/28/1998
> 155073040~04/29/1998
>
> Has 3 Id Numbers for the same data.
>
> If Id's are the same Pull Latest Data?
As you read the identifiers, separate the date from the data set id. Use
a hash keyed by the data set id to store an array of dates. Sort the
dates.
There are many other ways of doing this.
#!/usr/bin/perl
use strict;
use warnings;
my %dataset;
while ( my $id = <DATA> ) {
$id =~ s/^\s+//;
$id =~ s/\s+$//;
last unless length $id;
my ($set, $date) = split /~/, $id;
my ($m, $d, $y) = split '/', $date;
push @{ $dataset }, "$y/$m/$d";
}
print "Sets / dates (sorted by set)\n";
for my $set ( sort keys %dataset ) {
my @dates = sort { $b cmp $a } @{ $dataset };
$dataset = [ @dates ];
my $most_recent = shift @dates;
print(
join("\t", $set, $most_recent),
' ( ', join(',', @dates), ' ) ',
"\n",
);
}
my @sorted_sets = sort {
$dataset->[0] cmp $dataset->[0]
} keys %dataset;
print "Sets / dates (sorted by date of set)\n";
for my $set ( @sorted_sets ) {
my $most_recent = $dataset->[0];
print "$set\t$most_recent\n";
}
__DATA__
155073040~06/04/1998
155073040~04/28/1998
155073040~04/29/1998
255256040~04/29/1998
255293040~05/27/1999
255322040~12/09/1999
55322040~12/08/1999
755379040~04/30/1998
755383040~04/30/1998
755412040~01/19/1999
755612040~04/19/2000
755633040~04/26/1999
755763040~06/04/1998
--
(remove .invalid and reverse each component for email address)
comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
|