bug-bash
[Top][All Lists]

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

 From: Steven W. Orr Subject: Re: Negative indexes in address@hidden:off:length} Date: Mon, 27 Jun 2011 11:31:39 -0400 User-agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.18) Gecko/20110616 Thunderbird/3.1.11

```On 6/27/2011 10:25 AM, 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.

Chet

```
```
```
Can I suggest that all of this brouhaha is related to how python handles their arrayslice syntax?
```
foo=(22 33 44 55 66 77 88 99 111 222)

[x:y:z]
```
x is the start point. A negative index accesses elements from the end of the list counting backwards. The last element of any non-empty list is always x == -1. Or, foo[-n] == address@hidden - n]
```\${foo[-1]} is 222

```
y You can get a subset of a list, called a “slice”, by specifying two indices e.g., \${foo[1:2]} is 33, \${foo[-2:-1]} is 111
```
```
z is the 'stride', or how many you advance by. So, \${foo[::-1]} would be foo in reverse. \${foo[::2]} would just be the elements with even indices.
```
```
Don't know how much I'd use it. I also question how many people out there are chomping at the bit to write a numerical analysis lib in bash...
```

--
Time flies like the wind. Fruit flies like a banana. Stranger things have  .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net

```