[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: code snippet



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