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

Home » Public Forums » archive » last array index subscript
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
last array index subscript [message #33735] Thu, 23 January 2003 09:50 Go to next message
Chad Bender is currently offline  Chad Bender
Messages: 21
Registered: July 2001
Junior Member
Hi --

Is there a way in IDL to directly reference the last element of a vector
without first determining how many elements the vector contains?

For example, something like:

n=N_Elements(my_array)
value=my_array[n-1]

except without having to make the call to N_Elements. Granted, avoiding
the N_Elements call probably doesn't save a lot of time. But I figured
that with all of the complicated syntax that IDL accepts, there might be a
direct way to do this. The Bldg Apps guide says you can extract a
subarray from some element e to the end of the array like this:

subarray=my_array[e:*]

So it seems to me that IDL knows what that last index is. I tried
something like:

value=my_array[*-1], but (not unexpectedly) this caused a syntax error.

I figure that what I want is probably impossible, but if anyone knows how
to do it your advice is appreciated.

Thanks
Chad Bender
Re: last array index subscript [message #33830 is a reply to message #33735] Sat, 01 February 2003 21:20 Go to previous message
Mark Hadfield is currently offline  Mark Hadfield
Messages: 783
Registered: May 1995
Senior Member
"Vinay L. Kashyap" <kashyap@head-cfa.harvard.edu> wrote in message
news:3e3b0855.0@cfanews.harvard.edu...

> All I know is that it is a feature, not a bug.

Such a worthwhile feature that in IDL 5.6 a COMPILE_OPT keyword was
introduced to disable it!

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 Array
Subscripting. 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.

--
Mark Hadfield "Ka puwaha te tai nei, Hoea tatou"
m.hadfield@niwa.co.nz
National Institute for Water and Atmospheric Research (NIWA)
Re: last array index subscript [message #33834 is a reply to message #33735] Fri, 31 January 2003 15:35 Go to previous message
kashyap is currently offline  kashyap
Messages: 26
Registered: April 1993
Junior Member
Hello Doug,

All I know is that it is a feature, not a bug. Subscripting a
variable with an array of indices returns an array of the same
size. Where the index numbers undershoot (-ve values) the
output gets filled with the first element and where the index
numbers overshoot, those get filled out with the last element.

Look under the "Array Subscripts" section of the online help.

Vinay

In article <20030131103101140-0600@news.tc.umn.edu>,
Doug Rowland <rowland@fields.space.umn.edu> wrote:
> Vinay,
>
> How does this work? I tried to wrap my brain around this one. Is it an
> IDL "bug" or is there some simple rationale for IDL behaving this way?
> For example, if I try to directly subscript my_array with the scalar
> value 2147483647L (or any other number which is larger than the number
> of elements of my_array, less one) I get
>
> IDL> print,my_array[2147483647L]
> % Attempt to subscript A with <LONG ( 2147483647)> is out of range.
> % Execution halted at: $MAIN$
>
> Why should wrapping this index value in an array suddenly make it work?
>
> Thanks.
>
> Doug
>
> In <3e304dac.0@cfanews.harvard.edu> Vinay L. Kashyap wrote:
>> Try
>>
>> value=(my_array[[2147483647L]])[0]
>>
>> where the "[[.]]" returns an array and the "(.)[0]" ensures that the
>> output is a scalar, and 2147483647L = 2L^(31L)-1L is the largest
>> possible I*4 number you can have and surely no will have an array
>> bigger than that.
>>
>> vinay
--
____________________________________________________________ __________________
kashyap@head-cfa.harvard.edu 617 495 7173 [CfA/P-143] 617 496 7173 [F]
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: PNGs with alpha channel?
Next Topic: Polyfill, z-buffer & postscript

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

Current Time: Wed Oct 08 16:52:34 PDT 2025

Total time taken to generate the page: 0.00514 seconds