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

Home » Public Forums » archive » Re: code snippet
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: code snippet [message #25286] Wed, 30 May 2001 09:26
Brian Jackel is currently offline  Brian Jackel
Messages: 34
Registered: January 1998
Member
But then there's the following fun:

IDL> key= 1 & print,(['first','second','third','fourth'])[[key]]
second
IDL> key= 3 & print,(['first','second','third','fourth'])[[key]]
fourth
IDL> key= 5 & print,(['first','second','third','fourth'])[[key]]
fourth
IDL> key= -999 & print,(['first','second','third','fourth'])[[key]]
first

Which is a "feature" when you expect it, and a bug when you don't :)


Brian
Re: code snippet [message #25293 is a reply to message #25286] Tue, 29 May 2001 17:38 Go to previous message
m.hadfield is currently offline  m.hadfield
Messages: 36
Registered: April 2001
Member
From: "Craig Markwardt" <craigmnet@cow.physics.wisc.edu>
> choice = (['first','second','third','fourth'])[key]
> choice = (['first','second','third','fourth'])(key)
> choice = (['first','second','third','fourth'])([key])
> choice = (['first','second','third','fourth'])[(key)]
> One of these four expressions has a slightly different meaning than
> the other. As a trivia test to the newer users of IDL on the
> newsgroup (or the older ones!), can you say which one is different?

I think I qualify as one of the older ones.

IMHO you could (and should) eliminate some of the confusion by declaring
"compile_opt STRICTARR" or "compile_opt IDL2" in your startup file so that
numbers 2 and 3 are illegal.

Now, this

choice = (['first','second','third','fourth'])[key]

is the same as this

choice = (['first','second','third','fourth'])[(key)]

and indeed they are both the same as this

choice = (['first','second','third','fourth'])[((((((key))))))]

because all those parentheses around "key" have no effect.

But this

choice = (['first','second','third','fourth'])[[key]]

is different from all the others because the index is now a 1-D array, so
the result is also a 1-D array.

But what about these

choice = (['first','second','third','fourth'])[[[[[[key]]]]]]
choice = (['first','second','third','fourth'])[([([(key)])])]
choice = ([['first','second'],['third','fourth']])[[key]]
choice = ([['first','second','third'],'fourth'])[[key]]

I'll leave them as an exercise (I think they're all valid).

---
Mark Hadfield
m.hadfield@niwa.cri.nz http://katipo.niwa.cri.nz/~hadfield
National Institute for Water and Atmospheric Research



--
Posted from clam.niwa.cri.nz [202.36.29.1]
via Mailgate.ORG Server - http://www.Mailgate.ORG
Re: code snippet [message #25294 is a reply to message #25293] Tue, 29 May 2001 15:03 Go to previous message
Craig Markwardt is currently offline  Craig Markwardt
Messages: 1869
Registered: November 1996
Senior Member
"Dick Jackson" <dick@d-jackson.com> writes:
> choice = (['first','second','third','fourth'])[key]

Heh heh, and just to be archane, why not try these:

choice = (['first','second','third','fourth'])(key)
choice = (['first','second','third','fourth'])([key])
choice = (['first','second','third','fourth'])[(key)]

One of these four expressions has a slightly different meaning than
the other. As a trivia test to the newer users of IDL on the
newsgroup (or the older ones!), can you say which one is different?
[ And why ? ]

Craig


--
------------------------------------------------------------ --------------
Craig B. Markwardt, Ph.D. EMAIL: craigmnet@cow.physics.wisc.edu
Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response
------------------------------------------------------------ --------------
Re: code snippet [message #25295 is a reply to message #25294] Tue, 29 May 2001 14:21 Go to previous message
Dick Jackson is currently offline  Dick Jackson
Messages: 347
Registered: August 1998
Senior Member
Hi,

Ken Mankoff wrote...

> Hi everyone,
>
> i just ran across this in an IDL (its inside 'map_continents.pro')
> procedure, and thought its behaviour was pretty neat. [...]
>
> IDL> key = 2
> IDL> ;;; set 'key' to 0 or 1 or 2 or ...
> IDL> possibilities = ['first','second','third','fourth']
> IDL> choice = (possibilities)[key]
> IDL> help, choice
> CHOICE STRING = 'third'

Perhaps less neat is the (equivalent) simple array access:

choice = (possibilities)[key]

But neater still is subscripting an array built on-the-spot
(again equivalent to the original):

choice = (['first','second','third','fourth'])[key]

Note that all parentheses and brackets are required here.

Cheers,
--
-Dick

Dick Jackson / dick@d-jackson.com
D-Jackson Software Consulting / http://www.d-jackson.com
Calgary, Alberta, Canada / +1-403-242-7398 / Fax: 241-7392
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: how to set path?
Next Topic: Sorting and image rescaling

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

Current Time: Wed Oct 08 10:57:03 PDT 2025

Total time taken to generate the page: 0.00602 seconds