Assuming each line of your data is in @data_241 and NEW is the file handle you want to write to:
my %latest;
foreach my $line (@data_241) {
# split line
my ($id, $date) = split(/\s+/, $line);
if (! exists($latest{$id}) {
# place in hash if not seen before
$latest{$id} = $date;
} else {
# place in hash if date is greater than the one in hash
$latest{$id} = $date if
substr($date,4,4).substr($date,0,4) ge
substr($latest{$id},4,4).substr($latest{$id},0,4);
}
}
foreach my $key (sort keys %latest) {
# print out the latest values
print NEW "$key\t$latest{$key}\n";
}
-imran
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.