Basically, I have the following hash:
%db = ( 'scores' => { 'user1' => 200, 'user2' => 190, 'user3' => 232, 'user4' => 187, 'user5' => 190 } );
This hash is a list of scores that users have obtained at a certain activity. What I want to do with this hash is print out a "trophy stand" type of output, that shows the users in a list from highest score to lowest and their scores. All users need to be placed in their proper position (such as 1st and 2nd place). The hash is much bigger than this. Pretend there are at least 30 users in the hash. Some of this is simple to do, but there is one thing I could not bypass: the exception of multiple users who have the same score.
What I have right now basically outputs my example hash as:
What the output should really look like is this, where tied scores are put at the same level:1st Place: user3 (232 Points) 2nd Place: user1 (200 Points) 3rd Place: user2 (190 Points) 4th Place: user5 (190 Points) 5th Place: user4 (187 Points)
1st Place: user3 (232 Points) 2nd Place: user1 (200 Points) 3rd Place: user2 (190 Points), user5 (190 Points) 4th Place: user4 (187 Points)
Here is what I have for code:
my @totals = sort { $db{'scores'}{$b} <=> $db{'scores'}{$a} } keys(%{$ +db{'scores'}}); #Right here I need something that places all the users and their score +s in another hash #This new hash would probably be a hash of arrays. #I just can't figure it out! #Also, these lines here should not be hardcoded! #I do not know how many positions there are! #Meaning I manually outputted places 1st through to 5th, #but there might be 25 of them! #There should be some kind of loop that does it for me print qq| 1st Place: $totals[0] 2nd Place: $totals[1] 3rd Place: $totals[2] 4th Place: $totals[3] 5th Place: $totals[4] |;
I hope I was able to make myself clear on what I am asking for.
If anyone can get this for me, I will love you to death!
Yes, I am capable of loving someone so much that they die :)
In reply to Sorting Keys and Values by mt2k
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |