[Top][All Lists]

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

Re: Negative indexes in address@hidden:off:length}

From: Greg Wooledge
Subject: Re: Negative indexes in address@hidden:off:length}
Date: Mon, 27 Jun 2011 08:26:13 -0400
User-agent: Mutt/

On Sat, Jun 25, 2011 at 05:35:24PM +0200, address@hidden 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 "address@hidden:0:-1}" # Expected: c
>     echo "address@hidden:1:-2}" # Expected: a c

For the first one, you could use "address@hidden:(-1)}".

If I were trying to read "address@hidden: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, "address@hidden: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.

reply via email to

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