On Sat, Jun 25, 2011 at 05:35:24PM +0200, [email protected] wrote:
> arr=(a b c); echo "${arr[-1]}"
>
> In line with that, I'd like to propose extending that functionality to
> other operations that address array elements:
> echo "${arr[@]:0:-1}" # Expected: c
> echo "${arr[@]:1:-2}" # Expected: a c
For the first one, you could use "${arr[@]:(-1)}".
If I were trying to read "${arr[@]:0:-1}" and guess what it meant, I
would assume it meant to start with element 0 ("a") and go backward one
element (-1), which would either give the empty set or "a", depending
on how I chose to interpret it.
I don't even understand what the second one is supposed to mean at
all -- the :1: means to start with "b" and the -2 means to go back 2
elements...? How do you derive "a c" from any possible interpretation
of this?
If you want to assign a new meaning to a negative length, I would suggest
having it mean "iterate backwards". So, "${arr[@]:2:-3}" might give
"c b a". No idea how hard this would be to implement on Chet's end, but
it would give a way to reverse an array more easily than the "generate
a new array containing all the indices, then loop through the new array
in reverse" method that we give people in #bash.