[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Accessing Hash with Value



You could do something like this:

 foreach $key (sort sort_by_value keys %hash) {
   print "$key has $hash{$key}  votes";}

where sort_by_values is a subroutine sorting by values (for instance, 
something like $hash{$a} <=> %hash{$b}).

cheers,
Paul

On Tue, 22 Apr 1997, Chris Ouellette wrote:

> Hello There MacPerl. This is my first post, though I've been a longtime 
> lurker here on the MacPerl mailing list. This is a general perl question 
> more than a specific MacPerl question but anyway... what I am trying to 
> do is to come up with an easy way to handle poll data. I have set up a 
> hash with a key of the question and a value of the number of votes for 
> each question. I'm trying to output the results of this poll sorted by 
> the number of votes. What I can't seem to grasp is trying to print the 
> key when I do a sort based on value. Any help would be appreciated.
> 
> Code Snippet:
> -------------------------------------
> foreach $value (sort values %hash) {
> 	print "$key has $value votes";
> }
> --------------------------------------
> $key here has no assigned value. How can I get the key to print with it's 
> associated value?
> 
> -Chris Ouellette
> 
> ***** Want to unsubscribe from this list?
> ***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch
> 

***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch