You could use a HoA to store the data: col2 = key, col4 = values to be stored in the array. Then iterate thru the hash keys, send each value (an array) to a sub that returns the greatest value. The following does exactly that.
#!/usr/bin/perl
use Data::Dumper;
use strict;
my $k;
my %hash;
my @line;
# iterate thru data and store values in hash
while(<DATA>) {
chomp($_);
@line = split(",", $_);
push @{ $hash{$line[2]} }, $line[4];
}
# hash structure (in case you are curious)
print "hash structure:\n";
print Dumper(\%hash);
print "\n";
# iterate thru hash
# send arrays to greatest sub
print "greatest values for each key:\n";
foreach $k (keys %hash) {
print "$k: " . &greatest( $hash{$k} ) . "\n";
}
#return greatest value
sub greatest() {
my $arr = shift;
my @a = sort { $b <=> $a } @{ $arr };
return $a[0];
}
exit;
__DATA__
2004,21,21,1,16.00
2004,21,21,1,16.25
2004,22,22,1,18.00
2004,22,22,1,18.25
output:
hash structure:
$VAR1 = {
'22' => [
'18.00',
'18.25'
],
'21' => [
'16.00',
'16.25'
]
};
greatest values for each key:
22: 18.25
21: 16.25
hope this helps,
davidj
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.