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

Home » Public Forums » archive » Re: 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
Re: last array index subscript [message #33730] Thu, 23 January 2003 12:33 Go to next message
notspecified is currently offline  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 #33731 is a reply to message #33730] Thu, 23 January 2003 12:16 Go to previous messageGo to next message
kashyap is currently offline  kashyap
Messages: 26
Registered: April 1993
Junior Member
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

In article <Pine.LNX.4.33.0301231237280.25514-100000@hapuna.ess.sunysb.edu>,
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.
>
> Thanks
> Chad Bender
>
>


--
____________________________________________________________ __________________
kashyap@head-cfa.harvard.edu 617 495 7173 [CfA/P-143] 617 496 7173 [F]
Re: last array index subscript [message #33732 is a reply to message #33731] Thu, 23 January 2003 10:40 Go to previous messageGo to next message
James Kuyper is currently offline  James Kuyper
Messages: 425
Registered: March 2000
Senior Member
Chad Bender 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.

This doesn't have any advantages that I can see over the N_ELEMENTS
method; in fact, it's quite a bit less efficient; but it does work:

(reverse(my_array))[0]
Re: last array index subscript [message #33845 is a reply to message #33731] Fri, 31 January 2003 08:30 Go to previous message
Doug Rowland is currently offline  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
Re: last array index subscript [message #33847 is a reply to message #33730] Fri, 31 January 2003 08:24 Go to previous message
hieninger is currently offline  hieninger
Messages: 9
Registered: December 2002
Junior Member
Hi Matt,

> Matt Feinstein does not include his email address
> in the text of usenet postings.


...are you sure about that?!
;-))

Harro
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Cropping of IDLgrVolume?
Next Topic: Re: formatting array output?

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

Current Time: Wed Oct 08 18:13:30 PDT 2025

Total time taken to generate the page: 0.00687 seconds