bug-bash
[Top][All Lists]
Advanced

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

Re: array size vs index of last element (was Re: setarray[unsetkey] does


From: Chet Ramey
Subject: Re: array size vs index of last element (was Re: setarray[unsetkey] doesn't trigger nounset in arithmetic context)
Date: Mon, 29 May 2023 11:25:32 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.11.0

On 5/28/23 2:05 AM, Martin D Kealey wrote:
On Tue, 23 May 2023 at 23:32, Chet Ramey <chet.ramey@case.edu> wrote:

On 5/22/23 10:56 PM, Martin D Kealey wrote:

For example, if one is filling an array in random order, rather than
progressively adding to the end, then it is useful and makes sense to be
able to ask the array for its highest index.

You can parse the output of ${!array[@]} to get that.


True, but stupidly inefficient for a large array. (If you're inclined to
respond "but this is Shell so nobody cares about performance", please read
my rebuttal in the PS below.)

I suppose that if I had a large array, that I was populating randomly
with sparse elements, and I was interested in the highest-numbered index,
and I deemed the usual way to discover the largest index too inefficient,
I would keep track of that at assignment time.


How about making ${!array[@]:s:n} work in the obvious manner, including
when s=-1 n=1?

I don't generally jam together two different parameter expansions to create
a hybrid one, but if you'd like to take a shot at it, send me what you come
up with.

Chet
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/




reply via email to

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