bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: can a and d be the same for asort?


From: Wolfgang Laun
Subject: Re: can a and d be the same for asort?
Date: Mon, 5 Jul 2021 20:46:41 +0200

asort( source)
A) source is sorted, index values are lost, i.e., changed to 1, 2, 3,...

asort( source, dest )
*B) In this case, gawk copies the source array into the dest array and then
sorts dest, destroying its indices. However, the source array is not
affected. *

asort( source, source )
*C) In this case, gawk copies the source array into the  source array and
then sorts source, destroying its indices. However, the source array is not
affected. *

It should be immediately clear that the first phrase ("copies") and the
last sentence ("is not affected") state No-Ops.

An interesting point, though, is whether asort( a, a ) actually copies
array a into the same array. Does arrayA = arrayA do anything?

-W























On Mon, 5 Jul 2021 at 20:17, Andrew J. Schorr <
aschorr@telemetry-investments.com> wrote:

> On Mon, Jul 05, 2021 at 12:56:25PM -0500, Neil R. Ormos wrote:
> > I respectfully disagree that it's a pointless use
> > case.  The destination array argument must be
> > furnished in order to specify the "how" argument.
>
> Oops, you're right, that's a fair point.
>
> > If you want the original array sorted in-place, is
> > asort(a, a, "@val_num_desc") permitted?  Or do you
> > need to sort to a different destination array and
> > copy the contents back to a[]?  (I know the answer
> > (yes, no) in practice, but I don't think it's
> > unambiguous from the manual.)
>
> Yes. It's permitted. The code does the right thing.
>
> > Calling asort() without an explicitly named
> > destination array is (obviously) permitted.  But I
> > believe the manual does not say that "If you
> > supply a and d with the same value, it's just the
> > same as not supplying d at all."  (If I missed
> > that in the manual, I'm sorry.)  Maybe that
> > statement is all that's needed.
>
> Hmmm. It should work exactly as one would hope that it should, if I'm
> reading
> the code correctly. So should the docs be enhanced to allay one's fears
> that it
> might not work? One could always just try it. In my view, if it doesn't say
> that it's forbidden to use the same source and destination arguments, then
> one
> should assume that it's OK. We don't spell out every last detail. But I
> agree
> with your point that it is a relevant usage case; my earlier response was
> shortsighted for not recognizing that.
>
> Regards,
> Andy
>
>

-- 
Wolfgang Laun


reply via email to

[Prev in Thread] Current Thread [Next in Thread]