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 18:41:25 +0200

On 29 Jun 2011, at 15:01, Greg Wooledge wrote:
> 
> On Wed, Jun 29, 2011 at 01:42:06PM +0200, Maarten Billemont wrote:
>> 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

Firstly, I see my indenting of the "^ [label]" got whacked off target.  Let me 
try to correct that, if my MUA allows:

${arr[@]:1:2}

values:  [ a | b | c ]
indexes: 0   1   2   3
expand:      [ 1   2 ] => b c
             ^ start from 1
                   ^ length 2

${arr[@]:1:-2}

values:  [ a | b | c ]
indexes: 0   1   2   3
expand:    1 ]   [ 2   => a c
             ^ start from 1
                   ^ length 2

> But if you're starting with element 1, why wouldn't it be "b a"?

This is where preceding the element with the index comes in handy: We start 
with the index 1, which lays between element 0 and element 1.  We go back 1, 
that gives us element 0 and we're now between element 0 and the last element of 
the array (assuming a circular view on the array).  Go back one more and you 
now also get the last element in the array, and the cursor ends up between the 
last and the before last element.

For those to whom a cursor between elements seems confusing: think of your 
prompt's cursor and imagine an element is a single character.  Type this:

abcabc

Put your cursor before the second "a", imagine you're now at index 0:

abc|abc

Index 0 refers to element "a", because when you select 1 character 
(${arr[@]:0:1}), that'll be "a":

abc[a]bc.

Select 2 elements (${arr[@]:0:2}):

abc[ab]c.

Now try going backwards with your cursor to select 2 elements, instead of 
forwards (${arr[@]:0:-2}):

a[bc]abc

My example was ${arr[@]:1:-2}, so that would look like this:

ab[ca]bc


reply via email to

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