[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 165
  • Last Modified:

Easy Sorting Question

I'm sure this question has been asked and answered hundreds of times on EE, but in the time it would take to find it I can just ask it again...

I have a hash called players.  The keys are simply integers numbering the players.  The values are as follows:

$players{key}{name}
$players{key}{score}
$players{key}{losses}
.
.
.
etc.

I need to output a list to the user with all players and their corresponding values listed, but I want the list sorted by score.  I've written a bunch of simple sorts that are close, but they are not getting the job done.

Thanks,

Brian
0
BSwabby
Asked:
BSwabby
  • 2
1 Solution
 
helverCommented:
sub byscore {
    $players{$a}{score} <=> $players{$b}{score};
}

@sorted_keys_list = sort byscore (keys %players);
0
 
BSwabbyAuthor Commented:
Hi Helver-

Thanks for the code; it's exactly right.  The sick thing is I had the EXACT same sort code, but am having trouble printing the players and scores out of sorted_keys_list.  Any ideas on this one?  (I know it's rude to ask you a second question, but you're getting the points either way-- don't worry...)
0
 
ozoCommented:
foreach( @sorted_keys_list ){
  print "@{$players{$_}}{'name','score'}\n";
}
0
 
BSwabbyAuthor Commented:
Thanks to you both.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now