Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Only one of each 1

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I've got a scores script for a game i've made but it only shows one person for each number of kills.

You can see the result at




foreach my $key ( sort { $b <=> $a } keys %scores ) {
unless ($key == &quot;0&quot;) {
print scorehtm &quot;<TR><TD>$key<TD>$scores{$key}&quot;;
}
}

Anyone know why only one for each value is returned?
 
It looks like your %scores hash has a score
for a key, and one name as the value. The
problem is how you are assigning values to
the hash. Each key only occurs once, so if
you do something like:
Code:
$scores{56} = 'Fred';
$scores{56} = 'Barney';
The second assignment overwrites the first.

If I was doing this I think I'd just turn
the whole thing around and have the name as
the key and the score as the value. Then
do something like:
Code:
foreach my $key ( sort { $scores{$b} <=> $scores{$a} } keys %scores ) {
   unless ($key == &quot;0&quot;) {
      print scorehtm &quot;<TR><TD>$scores{$key}</TD><TD>$key</TD></TR>&quot;;
   }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top