Groups | Search | Server Info | Login | Register
Groups > comp.lang.perl.misc > #24883
| From | Rainer Weikusat <rweikusat@talktalk.net> |
|---|---|
| Newsgroups | comp.lang.perl.misc |
| Subject | Re: compare two sorted array, item by item, which one is bigger |
| Date | 2024-02-27 15:37 +0000 |
| Message-ID | <87ttltex4y.fsf@doppelsaurus.mobileactivedefense.com> (permalink) |
| References | <slrnutkg19.kdq.hymie@nasalinux.net> <87msrnyz6g.fsf@doppelsaurus.mobileactivedefense.com> |
Rainer Weikusat <rweikusat@talktalk.net> writes:
> 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;
> }
This can be simplified somewhat by using the <=> operator for the check
inside the loop as that already produces the desired result of either
-1, 0 or 1.
sub ary_cmp
{
my ($a0, $a1) = @_;
for (0 .. $#$a0) {
$_ and return $_ for $$a0[$_] <=> $$a1[$_];
}
return 0;
}
Back to comp.lang.perl.misc | Previous | Next — Previous in thread | Next in thread | Find similar
compare two sorted array, item by item, which one is bigger hymie! <hymie@nasalinux.net> - 2024-02-24 19:14 +0000
Re: compare two sorted array, item by item, which one is bigger Rainer Weikusat <rweikusat@talktalk.net> - 2024-02-26 16:20 +0000
Re: compare two sorted array, item by item, which one is bigger Rainer Weikusat <rweikusat@talktalk.net> - 2024-02-27 15:37 +0000
Re: compare two sorted array, item by item, which one is bigger hymie! <hymie@nasalinux.net> - 2024-02-28 15:35 +0000
Re: compare two sorted array, item by item, which one is bigger Rainer Weikusat <rweikusat@talktalk.net> - 2024-02-28 16:34 +0000
Re: compare two sorted array, item by item, which one is bigger Bouras George <g-bouras@otenet.gr> - 2024-05-20 13:09 +0300
csiph-web