PDA

View Full Version : Find last index



hcrisp
02-10-2009, 11:35 AM
A curious thing about WAVE is that array subscripts greater than the last subscript are automatically set to the last index if passed as an array.



WAVE> lmax = [2147483647L]
WAVE> x = findgen(137)
WAVE> info, x(lmax)
<Expression> FLOAT = Array(1)
WAVE> info, (x(lmax))(0)
<Expression> FLOAT = 136.000


Is there anything different with how the previous code gets executed than the standard way? Is it safe?



; standard way
WAVE> info, x(n_elements(x)-1)
<Expression> FLOAT = 136.000

rwagner
02-10-2009, 04:09 PM
Hi Hcrisp,
Yes, this is a language nuance we are pretty familiar with. It's safe in the sense that WAVE won't crash and acts this way consistently. I'll leave it up to you if you deem it safe in terms of using it your day to day code :D

-Ryan

totallyunimodular
02-11-2009, 10:33 AM
FYI that there is an analog to this behavior with negative numbers:

WAVE> x = INDGEN(10)
WAVE> PRINT, x
0 1 2 3 4 5 6 7 8 9
WAVE> PRINT, x([999])
9
WAVE> PRINT, x([-999])
0


These behaviors are clearly documented on page 87 of the Programmers Guide.