Re: last array index subscript [message #33730] |
Thu, 23 January 2003 12:33  |
notspecified
Messages: 14 Registered: February 2002
|
Junior Member |
|
|
On Thu, 23 Jan 2003 12:50:30 -0500, Chad Bender
<cbender@mail.astro.sunysb.edu> wrote:
> 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.
>
Well, sigh, how about
last = my_array[(size(my_array))[1]-1]
Matt Feinstein does not include his email address
in the text of usenet postings.
--------
Harvard Law of Automotive Repair: Anything that goes away
by itself will come back by itself.
|
|
|
|
|
Re: last array index subscript [message #33845 is a reply to message #33731] |
Fri, 31 January 2003 08:30  |
Doug Rowland
Messages: 12 Registered: March 2002
|
Junior Member |
|
|
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
>
--
------------------------------------------------------------ -------------------------------
Doug Rowland 472 Tate Laboratory of Physics
Space Physics Laboratory 116 Church Street S.E.
School of Physics and Astronomy Minneapolis, MN
University of Minnesota 55455
work: (612) 624-0378 email: rowland@fields.space.umn.edu
fax: (612) 624-4578
|
|
|
|