Re: OOB array indexing with an array gives no error message [message #15700] |
Fri, 04 June 1999 00:00 |
Liam Gumley
Messages: 473 Registered: November 1994
|
Senior Member |
|
|
Bobstrosity wrote:
> Array indexing is always a popular thread!
> It seems that you can index an array with a "bad value"
> if that bad value is an array rather than a scaler.
[examples removed]
This behavior is described in the IDL 5.2 online book 'Building IDL
Applications', in the section entitled 'Subscripts':
---begin quote---
If an attempt is made to reference a nonexistent element of an
array using a scalar subscript (a subscript that is negative or
larger than the size of the dimension minus 1), an error occurs
and program execution stops.
If an element of the subscript array is less than or equal to
zero, the first element of the subscripted variable is selected.
If an element of the subscript is greater than or equal to the
last subscript in the subscripted variable (N, above), the last
element is selected.
---begin quote---
In addition, regarding the use of trailing zero dimensions:
---begin quote---
When creating arrays, IDL eliminates all size 1, or
"degenerate", trailing dimensions. Thus, the statements
A = INTARR(10, 1)
HELP, A
print the following:
A INT = Array(10)
This removal of superfluous dimensions is usually convenient,
but it can cause problems when attempting to write fully general
procedures and functions. Therefore, IDL allows you to specify
"extra" dimensions for an array as long as the extra dimensions
are all zero. For example, consider a vector defined as follows:
ARR = INDGEN(10)
The following are all valid references to the sixth element of
ARR:
X = ARR[5]
X = ARR[5, 0]
X = ARR[5, 0, 0, *, 0]
Thus, the automatic removal of degenerate trailing dimensions
does not cause problems for routines that attempt to access the
resulting array.
---begin quote---
I guess the documentation is there for a reason after all. For those who
prefer a book made of paper, printable PDF versions of all the IDL
manuals are available at ftp://ftp.rsinc.com/pub/idl/info/docs/
Cheers,
Liam.
--
Liam E. Gumley
Space Science and Engineering Center, UW-Madison
http://cimss.ssec.wisc.edu/~gumley
|
|
|
Re: OOB array indexing with an array gives no error message [message #15701 is a reply to message #15700] |
Fri, 04 June 1999 00:00  |
davidf
Messages: 2866 Registered: September 1996
|
Senior Member |
|
|
Bobstrosity (dejastockwell@my-dejanews.com) writes:
> It seems that you can index an array with a "bad value"
> if that bad value is an array rather than a scaler.
>
> The Point: some code could potentially run off the end of
> an array by replicating the last part, without telling you.
It could indeed. On the other hand, I've seen many a good
IDL programmer use this feature to good advantage for
boundary error handling. :-)
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting
Phone: 970-221-0438 E-Mail: davidf@dfanning.com
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Toll-Free IDL Book Orders: 1-888-461-0155
|
|
|