comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: Strange arrary indexing quirk?
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: Strange arrary indexing quirk? [message #77178] Thu, 11 August 2011 22:05
Matt Francis is currently offline  Matt Francis
Messages: 94
Registered: May 2010
Member
Thanks for the help. That's a real doozy!
Re: Strange arrary indexing quirk? [message #77179 is a reply to message #77178] Thu, 11 August 2011 20:58 Go to previous message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Beaker writes:

> I stumbled across this strange behaviour:
>
> foo = indgen(2,2)
> print,foo[0,10]
> < gives an error, since the second index is out of range>
>
> however,
>
> indx = indgen(5)
> print,foo[0,indx]
> 0
> 2
> 2
> 2
> 2
>
> Does anyone have any insight into why this happens? I'd expect the
> second line to also throw an error. A lot of the inner workings of IDL
> seem pretty arcane and hard to guess at before you try things...
>
> I was being lazy/naughty and using a catch statement to detect when an
> index went out of range, which I knew would happen very occaisonally
> and was trying to avoid the performance hit of checking for it every
> time. This round off instead of an error behaviour made sure that plan
> didn't work!

This is a famous IDL quirk. If you would prefer to have
an error thrown in this situation, use the compile option
STRICTARRSUBS:

Compile_Opt STRICTARRSUBS

Looking up that compiler option will also point you to information
about array subscripts. (At least it will in the IDL 7 on-line help.)

Cheers,

David


--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
Re: Strange arrary indexing quirk? [message #77180 is a reply to message #77179] Thu, 11 August 2011 20:45 Go to previous message
penteado is currently offline  penteado
Messages: 866
Registered: February 2018
Senior Member
Administrator
On Aug 11, 10:12 pm, Beaker <mattjamesfran...@gmail.com> wrote:
> Does anyone have any insight into why this happens? I'd expect the
> second line to also throw an error. A lot of the inner workings of IDL
> seem pretty arcane and hard to guess at before you try things...

It happens because it was chosen that way when the language was
defined. Quoting from the help:

"STRICTARRSUBS — When IDL subscripts one array using another array as
the source of array indices, the default behavior is to clip any out-
of-range indices into range and then quietly use the resulting data
without error. This behavior is described in Understanding Array
Subscripts. Specifying STRICTARRSUBS will instead cause IDL to treat
such out-of-range array subscripts within the body of the routine
containing the COMPILE_OPT statement as an error. The position of the
STRICTARRSUBS option within the module is not important: All
subscripting operations within the entire body of the specified
routine will be treated this way."
Re: Strange arrary indexing quirk? [message #77181 is a reply to message #77180] Thu, 11 August 2011 20:42 Go to previous message
penteado is currently offline  penteado
Messages: 866
Registered: February 2018
Senior Member
Administrator
On Aug 11, 10:12 pm, Beaker <mattjamesfran...@gmail.com> wrote:
> Does anyone have any insight into why this happens? I'd expect the
> second line to also throw an error. A lot of the inner workings of IDL
> seem pretty arcane and hard to guess at before you try things...
>
> I was being lazy/naughty and using a catch statement to detect when an
> index went out of range, which I knew would happen very occaisonally
> and was trying to avoid the performance hit of checking for it every
> time. This round off instead of an error behaviour made sure that plan
> didn't work!

If you do not like that behaviour, use

compile_opt strictarraysubs
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Strange arrary indexing quirk?
Next Topic: iPlot Axes Colour

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 11:42:05 PDT 2025

Total time taken to generate the page: 0.00638 seconds