bug-bash
[Top][All Lists]
Advanced

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

Re: Negative indexes in ${arr[@]:off:length}


From: Maarten Billemont
Subject: Re: Negative indexes in ${arr[@]:off:length}
Date: Wed, 29 Jun 2011 13:42:06 +0200

On 27 Jun 2011, at 16:25, Chet Ramey wrote:
> 
>> 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?
> 
> I assume that he wants to be able to treat an indexed array as a circular
> buffer without having to do any of the work in a script.

Exactly, let's draw the array in the example:

arr=(a b c)
values:  [ a | b | c ]
indexes: 0   1   2   3

If you expand ${arr[@]:1:2}, you get the following:
values:  [ a | b | c ]
indexes: 0   1   2   3
expand:      [ 1   2 ] => b c
            ^ start from 1
                  ^ length 2

I propose we let a negative length iterate backward, so with ${arr[@]:1:-2}, 
you get the following:
values:  [ a | b | c ]
indexes: 0   1   2   3
expand:    1 ]   [ 2   => a c
            ^ start from 1
                  ^ length 2

Since ${arr[-1]} already does exactly this, I figure it would merely add to the 
syntax' consistency.


reply via email to

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