[Top][All Lists]

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

Feature request: index of index

From: Léa Gris
Subject: Feature request: index of index
Date: Thu, 6 May 2021 13:24:11 +0200
User-agent: Telnet/1.0 [tlh] (PDP11/DEC)

Currently, to know the index of an array's indexes (example: first or last index), it needs an intermediary index array:

#!/usr/bin/env bash

declare -a array=([5]=hello [11]=world [42]=here)
declare -ai indexes=("${!array[@]}")
declare -i first_index=${indexes[*]:0:1}
declare -i last_index=${indexes[*]: -1:1}
declare -p array indexes first_index last_index

Which prints:
declare -a array=([5]="hello" [11]="world" [42]="here")
declare -ai indexes=([0]="5" [1]="11" [2]="42")
declare -i first_index="5"
declare -i last_index="42"

It would be convenient to be able to index directly with this syntax:

declare -i first_index=${!array[@]:0:1}
declare -i last_index=${!array{@}: -1:1}

Léa Gris

reply via email to

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