On Tue, 23 May 2023 at 23:32, Chet Ramey <chet.ra...@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.) How about making ${!array[@]:s:n} work in the obvious manner, including when s=-1 n=1? -Marin PS: Everyone assumes that "the speed of external executables is the limiting factor", but it turns out that's not true. It's possible to do MUCH worse, just using built-in features: 27$ echo $BASH_VERSION > 5.1.4(47)-release > > 28$ grep -m1 'model name' /proc/cpuinfo > model name : Intel(R) Core(TM)2 Duo CPU T7100 @ 1.80GHz > > 29$ time A=({1..20000}) > > real 0m0.093s > user 0m0.076s > sys 0m0.008s > 30$ time B=( "${A[@]}" ) > > real 0m0.135s > user 0m0.120s > sys 0m0.004s > 31$ time I=( "${!A[@]}" ) > > real 0m0.135s > user 0m0.124s > sys 0m0.000s > 32$ time ls -ld . > drwxrwxr-x 7 martin martin 300 2023-05-28 00:50:48.683430004 +1000 ./ > > real 0m0.091s > user 0m0.008s > sys 0m0.068s > Now imagine doing that in a loop, and a script that takes HOURS instead of sub-second is entirely plausible. Or imagine running it on an older version of the shell: 16$ echo $BASH_VERSION > 4.0.0(58)-release > 17$ grep -m1 'model name' /proc/cpuinfo > model name : Intel(R) Core(TM)2 Duo CPU T7100 @ 1.80GHz > 18$ time A=({1..20000}) > > real 0m2.215s > user 0m2.172s > sys 0m0.008s > You have mail in /var/mail/martin > 19$ time B=( "${A[@]}" ) > > real 0m0.390s > user 0m0.344s > sys 0m0.016s > 20$ time I=( "${!A[@]}" ) > > real 0m2.754s > user 0m2.724s > sys 0m0.004s > 21$ time ls -ld . > drwxrwxr-x 7 martin martin 300 May 28 00:50 . > > real 0m0.069s > user 0m0.000s > sys 0m0.004s > 22$ > Yes, that's 40 times slower. -Martin