Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

File Help Please

0 views
Skip to first unread message

Andy

unread,
Apr 30, 2008, 5:15:17 PM4/30/08
to
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. Sinan Unur

unread,
Apr 30, 2008, 6:37:30 PM4/30/08
to
Andy <Ram...@gmail.com> wrote in news:d8bedb0c-5a45-4694-8ea3-
6ef4df...@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{$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 <1u...@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)

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

Gunnar Hjalmarsson

unread,
Apr 30, 2008, 10:12:57 PM4/30/08
to
Andy wrote:
> Hiya Guys
>
> Well I am new, and still trying to learn perl...

Do not multi-post!!
http://www.mail-archive.com/beginners%40perl.org/msg93766.html

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

0 new messages