Home > Archive > PERL Beginners > October 2006 > Perl DOOFUS needs help getting top 5 values from a hash
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Perl DOOFUS needs help getting top 5 values from a hash
|
|
| funnybroad@gmail.com 2006-10-30, 7:03 pm |
| I have reached maximum perl knowledge saturation... and I can't figure
this one out!
I just want to write some simple code to pull out a list of hash items
that have the top 5 highest cost.
The report should look like this (ok formatted better... but you get
the general idea):
Item Cost
car 21000.43
boat 15900.32
chair 105.42
lamp 46.28
shirt 45.34
I've tried pumping everything into an array starting with "cost" in the
string, sorting it descending, and shifting off the first 5 elements...
but I can't get it to sort properly, no matter how I format the "cost"
part with shiftf().
I can't do it with sort keys, because the costs are values, and not
keys.
Thank you to anyone who can help....
#! /usr/bin/perl
use Data::Dumper;
use strict;
use warnings;
my $item;
my $count;
my $cost;
my $csvline;
my %hashtest = ();
open CSVFILE, "testdata.csv" || die "couldn't open the file!";
while ($csvline = <CSVFILE> )
{
$csvline =~ /^(.*),(.*),(.*)$/;
($item,$count,$cost) = ($1,$2,$3);
$hashtest{$item}{count} = $count;
$hashtest{$item}{cost} = $cost;
}
close (CSVFILE);
print Dumper(%hashtest);
exit;
#Contents of csv file:
#book,2,10.56
#hat,4,5.92
#car,1,21000.43
#boat,2,15900.32
#shirt,5,45.34
#chair,9,105.42
#lamp,4,46.28
| |
| funnybroad@gmail.com 2006-10-30, 7:03 pm |
| p.s. Tried this, and it doesn't work:
foreach $key (sort {$hashtest{cost}{$b} <=> $hashtest{cost}{$a} } keys
%{$hashtest{cost}})
{
print "The key is $key \t";
print $hashtest{cost}, "\n";
}
funnybroad@gmail.com wrote:
> I have reached maximum perl knowledge saturation... and I can't figure
> this one out!
>
> I just want to write some simple code to pull out a list of hash items
> that have the top 5 highest cost.
>
> The report should look like this (ok formatted better... but you get
> the general idea):
>
> Item Cost
> car 21000.43
> boat 15900.32
> chair 105.42
> lamp 46.28
> shirt 45.34
>
> I've tried pumping everything into an array starting with "cost" in the
> string, sorting it descending, and shifting off the first 5 elements...
> but I can't get it to sort properly, no matter how I format the "cost"
> part with shiftf().
>
> I can't do it with sort keys, because the costs are values, and not
> keys.
>
> Thank you to anyone who can help....
>
> #! /usr/bin/perl
> use Data::Dumper;
> use strict;
> use warnings;
>
> my $item;
> my $count;
> my $cost;
> my $csvline;
>
> my %hashtest = ();
>
> open CSVFILE, "testdata.csv" || die "couldn't open the file!";
>
> while ($csvline = <CSVFILE> )
> {
> $csvline =~ /^(.*),(.*),(.*)$/;
> ($item,$count,$cost) = ($1,$2,$3);
> $hashtest{$item}{count} = $count;
> $hashtest{$item}{cost} = $cost;
> }
>
> close (CSVFILE);
>
> print Dumper(%hashtest);
>
> exit;
>
> #Contents of csv file:
> #book,2,10.56
> #hat,4,5.92
> #car,1,21000.43
> #boat,2,15900.32
> #shirt,5,45.34
> #chair,9,105.42
> #lamp,4,46.28
| |
| DJ Stunks 2006-10-30, 7:03 pm |
| funnybroad@gmail.com wrote:
> funnybroad@gmail.com wrote:
> p.s. Tried this, and it doesn't work:
>
> foreach $key (sort {$hashtest{cost}{$b} <=> $hashtest{cost}{$a} } keys
> %{$hashtest{cost}})
> {
> print "The key is $key \t";
> print $hashtest{cost}, "\n";
> }
>
>
[ please don't top post! ]
you were close on your sort try. try this:
#!/usr/bin/perl
use strict;
use warnings;
my %hash;
while (my $line = <DATA> ) {
my ($item, $count, $cost) = split /,/, $line;
@{ $hash{$item} }{'count','cost'} = ($count,$cost);
}
$, = $\ = "\n";
print sort { $hash{$a}{cost} <=> $hash{$b}{cost} } keys %hash;
__DATA__
book,2,10.56
hat,4,5.92
car,1,21000.43
boat,2,15900.32
shirt,5,45.34
chair,9,105.42
lamp,4,46.28
-jp
|
|
|
|
|