Groups | Search | Server Info | Login | Register


Groups > comp.lang.perl.misc > #24882

Re: compare two sorted array, item by item, which one is bigger

Path csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
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 Mon, 26 Feb 2024 16:20:55 +0000
Lines 68
Message-ID <87msrnyz6g.fsf@doppelsaurus.mobileactivedefense.com> (permalink)
References <slrnutkg19.kdq.hymie@nasalinux.net>
Mime-Version 1.0
Content-Type text/plain
X-Trace individual.net falAZ12vuJgAVoKwr//nLAJ6W3OIqHFPtim7g/6cHSXm9HdE4=
Cancel-Lock sha1:exmALpNCm7zrMDWqglRds8pKcVE= sha1:130kz3dWADQ8Z8RiQRjCzYvgQSk= sha256:Qa7hG8AwNirjj1FxFrfJmK5MxAEAAHlvSuwHAf1S8B4=
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Xref csiph.com comp.lang.perl.misc:24882

Show key headers only | View raw


hymie! <hymie@nasalinux.net> writes:
> 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?

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;
}

otherwise, it's a bit more difficult.

sub ary_cmp
{
    my ($a0, $a1) = @_;
    my ($last, $rc);

    $last = $#$a0;
    $_ < $last and $last = $_ for $#$a1;

    for (0 .. $last) {
        $rc = $$a0[$_] - $$a1[$_];
        return $rc < 0 ? -1 : 1 if $rc;
    }

    return @$a0 <=> @$a1;
}

could do.

Back to comp.lang.perl.misc | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

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