char09 has asked for the wisdom of the Perl Monks concerning the following question:

Thanks for the help but how do I specify the event. This is what I had. $party='d'; $Statistics = 'a=0,b=2,c=0,d=6,-=0'; foreach my $stats (split(',', $Statistics)) { print $stats, " \n"; my ($event, $occurance) = split('=', $stats); $HASH{$event} = $occurance; } while ( ($event, $occurance) = each %HASH ) { sub hashValueNum { $HASH{$b} <=> $HASH{$a}; } print "$event equals $occurance.\n"; } foreach $event (sort hashValueNum (keys(%HASH))) { print "\t\t$HASH{$event} \t\t $event\n"; } if (exists ($HASH{$party})) { print ' yay! '; } I want to look in the hash for an event, $party and if $party is their then I want to use the occurance of party to do the calculation. Utilitarian how can I do that in your code?

Replies are listed 'Best First'.
Re: Help with hash
by Corion (Patriarch) on Apr 29, 2009 at 08:08 UTC
Re: Help with hash
by Utilitarian (Vicar) on Apr 29, 2009 at 08:50 UTC
    Quick and dirty example follows:
    perl -e '%events=qw (A 0 B 0 D 12 C 30 D 50); $max=0;$total=0;foreach + (keys (%events)){$total += $events{$_};$max=$events{$_} if ($events{ +$_}>$max);}foreach (sort {$events{$b} <=> $events{$a} } keys (%events +)){$inv_pc=$max*100/$events{$_} if ($events{$_});print "$_ : $inv_pc +:", $events{$_}*100/$total,"\n"}' D : 100 :62.5 C : 166.666666666667 :37.5 A : 166.666666666667 :0 B : 166.666666666667 :0