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.
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?
hymie! <hymie@nasalinux.net> writes:
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?
If you're arrays are always of equal length, you could use
sub ary_cmp
{
my ($a0, $a1) = @_;
my $rc;
for (0 .. $#$a0) {
$rc = $$a0[$_] - $$a1[$_];
return $rc < 0 ? -1 : 1 if $rc;
}
return 0;
}
hymie! <hymie@nasalinux.net> writes:
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?
If your arrays are always of equal length, you could use
otherwise, it's a bit more difficult.
sub ary_cmp
{
my ($a0, $a1) = @_;
my ($last, $rc);
# we need the length of the shorter array
# start with the length of array a0
# and see if the length of array a1 is less
$last = $#$a0;
$_ < $last and $last = $_ for $#$a1;
# for each entry in the shorter array
# compare that numbered entry in the two arrays
# return <=> if the result is not 0
for (0 .. $last) {
$_ and return $_ for $$a0[$_] <=> $$a1[$_];
}
# all of the elements are equal, so return the longer array
return @$a0 <=> @$a1;
}
for (0 .. $last) {
$_ and return $_ for $$a0[$_] <=> $$a1[$_];
In our last episode, the evil Dr. Lacto had captured our hero,
Rainer Weikusat <rweikusat@talktalk.net>, who said:
hymie! <hymie@nasalinux.net> writes:
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?
If your arrays are always of equal length, you could use
I don't think I can depend on that :(
otherwise, it's a bit more difficult.
sub ary_cmp
{
my ($a0, $a1) = @_;
my ($last, $rc);
# we need the length of the shorter array
# start with the length of array a0
# and see if the length of array a1 is less
$last = $#$a0;
$_ < $last and $last = $_ for $#$a1;
# for each entry in the shorter array
# compare that numbered entry in the two arrays
# return <=> if the result is not 0
for (0 .. $last) {
$_ and return $_ for $$a0[$_] <=> $$a1[$_];
}
# all of the elements are equal, so return the longer array
return @$a0 <=> @$a1;
}
I took the liberty of adding your improvement to this function.
I'll definitely try this out and see how well it work.
I have a few followup questions...
(*) I added some comments. Can you tell me if I'm correct?
(*) Could I have set $last this way?
$last = $#$a0 < $#$a1 ? $#$a0 : $#$a1 ;
or
$last = @$a0 < @$a1 ? @$a0 : @$a1 ;
?
(*) In this construct
for (0 .. $last) {
$_ and return $_ for $$a0[$_] <=> $$a1[$_];
is it safe to reuse $_ like that?
I'm sure this is an FAQ if I can just find the correct words to ask my question.
We must stop writing code no matter the language , everything is about
the right data structures for every specific problem.
Sysop: | DaiTengu |
---|---|
Location: | Appleton, WI |
Users: | 991 |
Nodes: | 10 (1 / 9) |
Uptime: | 76:27:40 |
Calls: | 12,949 |
Calls today: | 3 |
Files: | 186,574 |
Messages: | 3,264,545 |