Subscripting arrays. [message #84150] |
Thu, 02 May 2013 12:40  |
Russell Ryan
Messages: 122 Registered: May 2012
|
Senior Member |
|
|
So someone showed me this and I'm trying to wrap my head around it. Sorry if it's already posted, I didn't know how to begin to search The Group for it...
Create an array:
a = intarr(3)
Using IDL 8, subscript with 4 (ie. an index that's too large and should be out of bounds)
a[4]=3
Obviously, this crashes.
Conversely subscript with a negative index:
a[-1]=2
does what you expect.
But, now subscript the array, WITH an array:
a[[4]]=2
voila. It works, and simply truncates to the last element of the array... Spooky. Now do that with a negative index:
a[[-1]]=4
Like before, it works. But now, it doesn't wrap the index, but rather truncates to the first element of the array.
Hmm.. I can't tell if this is the designed behavior, because I have a hard time describing it --- it's easier to show it.
All the best,
Russell
|
|
|
Re: Subscripting arrays. [message #84211 is a reply to message #84150] |
Mon, 06 May 2013 06:37  |
Jeremy Bailin
Messages: 618 Registered: April 2008
|
Senior Member |
|
|
On 5/3/13 4:09 PM, rryan@stsci.edu wrote:
> A ha. Sounds reasonable, but I'm not sure why it's "designed" this way, but c'est la vie. I passed your response on to the friend, who groaned. We nearly said (in unison), I never pay much attention to those compiler options.
>
> Thanks!
> Russell
There are times when I think this is the best feature ever. There are
also times when I swear profusely at it. What would be really cool would
be if you could switch it on and off for a particular indexing operation
at runtime, but I'm sure that would be a nightmare to implemenet!
-Jeremy.
|
|
|