Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

God made machine language; all the rest is the work of man.


devel / comp.lang.perl.misc / Re: Sorting an array by two functions

SubjectAuthor
* Sorting an array by two functionsHenry Law
+- Re: Sorting an array by two functionsKeith Thompson
+* Re: Sorting an array by two functionsRainer Weikusat
|`- Re: Sorting an array by two functionsHenry Law
`- Re: Sorting an array by two functionsAndrzej Adam Filip

1
Sorting an array by two functions

<oIudnW-vJaaKwz_8nZ2dnUU78V_NnZ2d@giganews.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=245&group=comp.lang.perl.misc#245

  copy link   Newsgroups: comp.lang.perl.misc
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!3.eu.feeder.erje.net!feeder.erje.net!border1.nntp.ams1.giganews.com!nntp.giganews.com!buffer1.nntp.ams1.giganews.com!news.giganews.com.POSTED!not-for-mail
NNTP-Posting-Date: Sat, 27 Nov 2021 10:34:31 -0600
From: news@lawshouse.org (Henry Law)
Subject: Sorting an array by two functions
Newsgroups: comp.lang.perl.misc
User-Agent: Pan/0.145 (Duplicitous mercenary valetism; d7e168a
git.gnome.org/pan2)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Message-ID: <oIudnW-vJaaKwz_8nZ2dnUU78V_NnZ2d@giganews.com>
Date: Sat, 27 Nov 2021 10:34:31 -0600
Lines: 21
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-sA7JndP1Ax8w9DWwWFcPcUxXh3NjDysRGFsmpzw3dC5an+2y28p26kHfOrK7Yd1MooDSK3mmMY2Skfx!M6MU3B8sKrPjIXGbmR5qMm+KA4uhmje7EYs4bNjZrhTadf3OpaWXK37Z3rU4dvAYi7xANugpuNHz
X-Complaints-To: abuse@giganews.com
X-DMCA-Notifications: http://www.giganews.com/info/dmca.html
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
X-Original-Bytes: 1805
 by: Henry Law - Sat, 27 Nov 2021 16:34 UTC

I have an array of arrayrefs with this structure:

@array = ( [ '20100403', 'text', 'account code' ],
[ '19990503', 'text', 'other account code' ],
... etc )

I want to sort by account code and then by date. I read in various
places that the following will do the trick:

@array = sort { $a->[2] cmp $b->[2] || $a->[0] <=> $b->[0] } @array;

But I'm not getting the sort order I want. I know that the "sort"
function requires a three-valued return from the block, -1, 0 or +1; but
the "||" operator won't do that; it returns TRUE or FALSE.

Is that understanding correct? Is that the reason that I'm not getting
the order I want?

--
Henry Law n e w s @ l a w s h o u s e . o r g
Manchester, England

Re: Sorting an array by two functions

<87sfvhff1z.fsf@nosuchdomain.example.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=246&group=comp.lang.perl.misc#246

  copy link   Newsgroups: comp.lang.perl.misc
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Keith.S.Thompson+u@gmail.com (Keith Thompson)
Newsgroups: comp.lang.perl.misc
Subject: Re: Sorting an array by two functions
Date: Sat, 27 Nov 2021 13:45:44 -0800
Organization: None to speak of
Lines: 49
Message-ID: <87sfvhff1z.fsf@nosuchdomain.example.com>
References: <oIudnW-vJaaKwz_8nZ2dnUU78V_NnZ2d@giganews.com>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="d181ea83918afdfee5a43b4a10b0e6f8";
logging-data="13231"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19AlVL8dv4xu94E0o7DaPzr"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:FDraFp3trDoVKXLrSmMQjS+nfh0=
sha1:ZhN3BvemmCGkhQUimVRiHStvKPc=
 by: Keith Thompson - Sat, 27 Nov 2021 21:45 UTC

Henry Law <news@lawshouse.org> writes:
> I have an array of arrayrefs with this structure:
>
> @array = ( [ '20100403', 'text', 'account code' ],
> [ '19990503', 'text', 'other account code' ],
> ... etc )
>
> I want to sort by account code and then by date.

Do you want elements with the same account code and different dates
grouped together, or vice versa?

> I read in various
> places that the following will do the trick:
>
> @array = sort { $a->[2] cmp $b->[2] || $a->[0] <=> $b->[0] } @array;
>
> But I'm not getting the sort order I want. I know that the "sort"
> function requires a three-valued return from the block, -1, 0 or +1;

The function has to return a value that's less than, equal to, or
greater than zero. It doesn't have to be -1, 0, or +1 (though of course
it can be, and "<=>" and "cmp" do so unless you use "<=>" with NaNs).
See "perldoc perlop".

> but
> the "||" operator won't do that; it returns TRUE or FALSE.
>
> Is that understanding correct? Is that the reason that I'm not getting
> the order I want?

No. Quoting "perldoc perlop":

The "||", "//" and "&&" operators return the last value evaluated
(unlike C's "||" and "&&", which return 0 or 1).

(And note that Perl doesn't have TRUE and FALSE constants.)

I don't know what the problem is. I'd probably use "or" rather than
"||", or add parentheses, just for greater clarity, but "cmp" and "<=>"
bind more tightly than either "||" or "or", so that's not the issue.

If you don't get an answer from someone else, try showing us some sample
data, the order you expect, and the order you're getting.

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */

Re: Sorting an array by two functions

<87v90azw5j.fsf@doppelsaurus.mobileactivedefense.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=247&group=comp.lang.perl.misc#247

  copy link   Newsgroups: comp.lang.perl.misc
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: rweikusat@talktalk.net (Rainer Weikusat)
Newsgroups: comp.lang.perl.misc
Subject: Re: Sorting an array by two functions
Date: Mon, 29 Nov 2021 17:52:56 +0000
Lines: 36
Message-ID: <87v90azw5j.fsf@doppelsaurus.mobileactivedefense.com>
References: <oIudnW-vJaaKwz_8nZ2dnUU78V_NnZ2d@giganews.com>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: individual.net AlUzBfZB2mKWVHTjeB2aUAhS3dmYQ3hjmAJZwrNvXpq3uAnzI=
Cancel-Lock: sha1:dHeeqeT/cy3Fa0Uc/gtoK/g+16A= sha1:Kr6YyZhru5ggxPxGblyTnbUbTps=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux)
 by: Rainer Weikusat - Mon, 29 Nov 2021 17:52 UTC

Henry Law <news@lawshouse.org> writes:
> I have an array of arrayrefs with this structure:
>
> @array = ( [ '20100403', 'text', 'account code' ],
> [ '19990503', 'text', 'other account code' ],
> ... etc )
>
> I want to sort by account code and then by date. I read in various
> places that the following will do the trick:
>
> @array = sort { $a->[2] cmp $b->[2] || $a->[0] <=> $b->[0] } @array;
>
> But I'm not getting the sort order I want. I know that the "sort"
> function requires a three-valued return from the block, -1, 0 or +1; but
> the "||" operator won't do that; it returns TRUE or FALSE.
>
> Is that understanding correct? Is that the reason that I'm not getting
> the order I want?

The code below works as expected:

-----
my @a;

for (reverse('a' .. 'z'), 'a' .. 'z') {
push(@a, [$_, int(rand(10))]);
}

sub pa
{ print($$_[0], "\t", $$_[1], "\n") for @_;
}

my @b = sort { $$a[0] cmp $$b[0] || $$a[1] <=> $$b[1] } @a;

pa(@b);

Re: Sorting an array by two functions

<VtKdnWcIE6SIxDj8nZ2dnUU78VudnZ2d@giganews.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=248&group=comp.lang.perl.misc#248

  copy link   Newsgroups: comp.lang.perl.misc
Path: i2pn2.org!i2pn.org!aioe.org!feeder1.feed.usenet.farm!feed.usenet.farm!news-out.netnews.com!news.alt.net!fdc2.netnews.com!peer02.ams1!peer.ams1.xlned.com!news.xlned.com!border2.nntp.ams1.giganews.com!nntp.giganews.com!buffer2.nntp.ams1.giganews.com!news.giganews.com.POSTED!not-for-mail
NNTP-Posting-Date: Mon, 29 Nov 2021 16:49:57 -0600
From: news@lawshouse.org (Henry Law)
Subject: Re: Sorting an array by two functions
Newsgroups: comp.lang.perl.misc
References: <oIudnW-vJaaKwz_8nZ2dnUU78V_NnZ2d@giganews.com>
<87v90azw5j.fsf@doppelsaurus.mobileactivedefense.com>
User-Agent: Pan/0.145 (Duplicitous mercenary valetism; d7e168a
git.gnome.org/pan2)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Message-ID: <VtKdnWcIE6SIxDj8nZ2dnUU78VudnZ2d@giganews.com>
Date: Mon, 29 Nov 2021 16:49:57 -0600
Lines: 22
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-BNPp9TFLEGYnaRtKndOobK3mdSlFjxms2qw+VIunUuQx5DHUPYA8JAn3cSgdCueLupY8pq/uvSqH3N8!6gXr4sCOG4xY074netpCz6bLXqR7BzKhBbqG50eL+5nuLlwPx0xNILTMcWDPmfKeOqwy2oofEDY=
X-Complaints-To: abuse@giganews.com
X-DMCA-Notifications: http://www.giganews.com/info/dmca.html
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
X-Original-Bytes: 1946
X-Received-Bytes: 2037
 by: Henry Law - Mon, 29 Nov 2021 22:49 UTC

On Mon, 29 Nov 2021 17:52:56 +0000, Rainer Weikusat wrote:

> The code below works as expected:

Indeed it does; and so does mine now that I've found the unrelated
mistake.

And I understand the error in my original reasoning. For anyone other
than me dim enough to need an explanation:

> my @b = sort { $$a[0] cmp $$b[0] || $$a[1] <=> $$b[1] } @a;

"||" operator returns the last value evaluated (see perlop); so if the
first comparison comes up zero and the second as, say, -1, the "||"
operation becomes 0 || -1; the result is -1 and the sort is satisfied.

I thought it would be restricted to 0 or 1, as in C; perlop clearly draws
attention to this distinction.

--
Henry Law n e w s @ l a w s h o u s e . o r g
Manchester, England

Re: Sorting an array by two functions

<anfi+vpjx64kslf-lbu2@wp.eu>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=249&group=comp.lang.perl.misc#249

  copy link   Newsgroups: comp.lang.perl.misc
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: anfi@onet.eu (Andrzej Adam Filip)
Newsgroups: comp.lang.perl.misc
Subject: Re: Sorting an array by two functions
Date: Tue, 30 Nov 2021 00:15:24 +0000 (UTC)
Organization: It is for me to know and for you to find out.
Lines: 28
Message-ID: <anfi+vpjx64kslf-lbu2@wp.eu>
References: <oIudnW-vJaaKwz_8nZ2dnUU78V_NnZ2d@giganews.com>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="6ae2ad1df6ab8af92e3b20c446f4cd2f";
logging-data="3982"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+nTgawKuFD+U4uSgyrfYKh"
Cancel-Lock: sha1:mS8I5O0bbSN+h/tfp3KWnh5xR2s=
sha1:cz6AzZoBSOrBYZSTsJTcmC1wAHI=
 by: Andrzej Adam Filip - Tue, 30 Nov 2021 00:15 UTC

Henry Law <news@lawshouse.org> wrote:
> I have an array of arrayrefs with this structure:
>
> @array = ( [ '20100403', 'text', 'account code' ],
> [ '19990503', 'text', 'other account code' ],
> ... etc )
>
> I want to sort by account code and then by date. I read in various
> places that the following will do the trick:
>
> @array = sort { $a->[2] cmp $b->[2] || $a->[0] <=> $b->[0] } @array;
>
> But I'm not getting the sort order I want. I know that the "sort"
> function requires a three-valued return from the block, -1, 0 or +1; but
> the "||" operator won't do that; it returns TRUE or FALSE.
>
> Is that understanding correct? Is that the reason that I'm not getting
> the order I want?

man perlop
The "||", "//" and "&&" operators return the last value evaluated
(unlike C's "||" and "&&", which return 0 or 1).

One-liner below produces "-1-1" :
perl -e 'print( -1 || 0, 0||-1)'

--
[Andrew] Andrzej A. Filip


devel / comp.lang.perl.misc / Re: Sorting an array by two functions

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor