hymie!
2024-02-24 19:14:49 UTC
I'm sure this is an FAQ if I can just find the correct words to ask my
question.
I have two people, 0 and 1, which are denoted by the $player variable.
I have a hash of sorted arrays
@{$scores{$player}}
104 92 92 90 87
104 92 92 89 88
And I have a %percent hash that holds the sum of those elements.
In this case, $percent{$player} is 465 for both.
(I can safely assume that all numbers are non-negative, so I'm fine with
an empty value being treated as zero)
(I also have a bunch of lousy code that I'm not proud of)
So I have this construct
foreach $player (sort {$percent{$b} <=> $percent{$a}} keys %percent)
that will sort the %percent hash by value ... but since the two are
equal, I think I'm getting a random choice.
So then I wrote this construct
foreach $player (sort
{$percent{$b} <=> $percent{$a} || ${$scores{$b}}[0] <=> ${$scores{$a}}[0] }
keys %percent)
which will check the first element in each array from the %scores hash
to see which value is larger.
The question is -- how can I (or can I) programatically keep checking
entries in the arrays of the %scores hash until I find a pair of
entries that are not equal? I'd rather not have (in my case) 9
individual tests of the items. Is there a simple subroutine I can
use?
My first thought is to compare $a[0] and $b[0] ... and then if they're
the same, shift them both and try again. But I'm a little nervous that
shifting the arrays will lose the values, and I don't want to do that.
My second thought is use an array slice -- if $a[0] == $b[0] then
recursively check $a[1-x] against $b[1-x] ...
I'm hoping somebody knows something simpler.
Thanks.
--hymie! https://nasalinux.net/~hymie ***@nasalinux.net
question.
I have two people, 0 and 1, which are denoted by the $player variable.
I have a hash of sorted arrays
@{$scores{$player}}
104 92 92 90 87
104 92 92 89 88
And I have a %percent hash that holds the sum of those elements.
In this case, $percent{$player} is 465 for both.
(I can safely assume that all numbers are non-negative, so I'm fine with
an empty value being treated as zero)
(I also have a bunch of lousy code that I'm not proud of)
So I have this construct
foreach $player (sort {$percent{$b} <=> $percent{$a}} keys %percent)
that will sort the %percent hash by value ... but since the two are
equal, I think I'm getting a random choice.
So then I wrote this construct
foreach $player (sort
{$percent{$b} <=> $percent{$a} || ${$scores{$b}}[0] <=> ${$scores{$a}}[0] }
keys %percent)
which will check the first element in each array from the %scores hash
to see which value is larger.
The question is -- how can I (or can I) programatically keep checking
entries in the arrays of the %scores hash until I find a pair of
entries that are not equal? I'd rather not have (in my case) 9
individual tests of the items. Is there a simple subroutine I can
use?
My first thought is to compare $a[0] and $b[0] ... and then if they're
the same, shift them both and try again. But I'm a little nervous that
shifting the arrays will lose the values, and I don't want to do that.
My second thought is use an array slice -- if $a[0] == $b[0] then
recursively check $a[1-x] against $b[1-x] ...
I'm hoping somebody knows something simpler.
Thanks.
--hymie! https://nasalinux.net/~hymie ***@nasalinux.net