[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Feature request: index of index
From: |
Phi Debian |
Subject: |
Re: Feature request: index of index |
Date: |
Tue, 22 Jun 2021 18:21:02 +0200 |
Meanwhile you may use functions to setup your variables something along
those lines.
PW$ function first-index { echo $1; }
PW$ function last-index { shift $(($#-1)) ; echo $1; }
PW$ declare -a array=([5]="hello" [11]="world" [42]="here")
PW$ declare -i first_index=$(first-index ${!array[@]})
PW$ declare -i last_index=$(last-index ${!array[@]})
PW$ echo $first_index $last_index
5 42
On Thu, May 6, 2021 at 1:24 PM Léa Gris <lea.gris@noiraude.net> wrote:
> 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
>
>
>
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- Re: Feature request: index of index,
Phi Debian <=