BlenderHead has asked for the wisdom of the Perl Monks concerning the following question:
Hello World:
I have the following code written to generate a hash:
my $count = 1; my @array = ('A','B','C'); my %hash = (); for my $i (@array) { # set key AND initialize by $count as value of key. $hash{$i} = $count; $count++; }
Now, what I want to do is sort the hash by values and print it out, but my print loop isnt working right. I can get the values out okay, but the keys are not working to print.
Here's what I have.
for my $key ( sort {$hash{$a} <=> $hash{$b} } keys %hash ) { print " MIRACLE_HAPPENS is equal to $hash{$key}.<br>\n"; }
As you can see - it wont print the key name. It'll only print the sorted values. Where it says "MIRACLE HAPPENS" is where I need it to print the key name. Can anyone show me how to do that please?
Thanks!
BH
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Simple Hash Question
by kennethk (Abbot) on Nov 18, 2009 at 21:52 UTC | |
|
Re: Simple Hash Question
by BioLion (Curate) on Nov 18, 2009 at 21:56 UTC | |
by johngg (Canon) on Nov 18, 2009 at 22:44 UTC | |
by BlenderHead (Novice) on Nov 18, 2009 at 22:19 UTC | |
|
Re: Simple Hash Question
by ikegami (Patriarch) on Nov 18, 2009 at 22:12 UTC |