File Help Please

A

Andy

Hiya Guys

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..

Needless to say

I need to accomplish the following.
Data Below

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

this is an example File that gets updated

Basically I need to be able to pull the latest data.

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?

so If I pulled this I would only get

155073040~06/04/1998

Any help would be appreciated.
 
A

A. Sinan Unur

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{$set} }, "$y/$m/$d";
}

print "Sets / dates (sorted by set)\n";

for my $set ( sort keys %dataset ) {
my @dates = sort { $b cmp $a } @{ $dataset{$set} };
$dataset{$set} = [ @dates ];
my $most_recent = shift @dates;

print(
join("\t", $set, $most_recent),
' ( ', join(',', @dates), ' ) ',
"\n",
);
}

my @sorted_sets = sort {
$dataset{$b}->[0] cmp $dataset{$a}->[0]
} keys %dataset;

print "Sets / dates (sorted by date of set)\n";


for my $set ( @sorted_sets ) {
my $most_recent = $dataset{$set}->[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


--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top