Click here to get back home

File Help Please

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
File Help Please Andy 04-30-2008
`--> Re: File Help Please Gunnar Hjalmars...04-30-2008
Posted by Andy on April 30, 2008, 5:15 pm
Please log in for more thread options
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.

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/

Posted by Gunnar Hjalmarsson on April 30, 2008, 10:12 pm
Please log in for more thread options
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

Similar ThreadsPosted
FAQ 5.2: How do I change one line in a file/delete a line in a file/insert a line in the middle of a file/append to the beginning of a file? December 7, 2004, 6:03 am
FAQ 5.2 How do I change one line in a file/delete a line in a file/insert a line in the middle of a file/append to the beginning of a file? January 27, 2005, 12:03 pm
FAQ 5.2 How do I change one line in a file/delete a line in a file/insert a line in the middle of a file/append to the beginning of a file? April 17, 2005, 11:03 am
FAQ 5.2 How do I change one line in a file/delete a line in a file/insert a line in the middle of a file/append to the beginning of a file? July 3, 2005, 10:03 am
FAQ 5.2 How do I change one line in a file/delete a line in a file/insert a line in the middle of a file/append to the beginning of a file? September 10, 2005, 4:03 pm
FAQ 5.2 How do I change one line in a file/delete a line in a file/insert a line in the middle of a file/append to the beginning of a file? April 27, 2006, 9:03 am
FAQ 5.2 How do I change one line in a file/delete a line in a file/insert a line in the middle of a file/append to the beginning of a file? August 14, 2006, 9:03 am
FAQ 5.2 How do I change one line in a file/delete a line in a file/insert a line in the middle of a file/append to the beginning of a file? November 9, 2006, 9:03 am
Perl/unix script to convert a fixed width file to a tab delimited file September 29, 2007, 9:35 am
How can I send the output of the system() command to a file and capture a string in that file June 19, 2005, 1:30 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap