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

Re: assignment inside boolean expression



Ben Tupper wrote:
> 
> Craig Markwardt wrote:
> [...]
> > Not to undercut you, but will (X AND 1) do the trick?
> >
> 
> Thanks to Ken and Craig. I think for my purposes the following should suffice (I
> guess as long as I make sure that I'm working with an integer/long/byte type.)
> 
> X = Indgen(6) - 2
> 
> For i = 0, N_elements(X)-1 Do $
>    If X[i] then Print, X[i], ': Odd' Else print, X[i], ': Even'
> 
> -2: Even
> -1: Odd
> 0: Even
> 1: Odd
> 2: Even
> 3: Odd
> 
> Thanks again,
> 
> Ben

but if you start increasing the number of elements of X to say 1000000,
you are
certainly better off with:
   answer=['even','odd']
   print,answer[ (x and 1) ]

no loop ;-)

Example:
IDL> x=lindgen(20)-5 
IDL> answer=['even','odd']
IDL> print,answer[x and 1]
odd even odd even odd even odd even odd even odd even odd even odd even
odd even odd even

BTW: X MOD 2   does not work for negative numbers !!!
IDL> print,answer[x mod 2]
even even even even even even odd even odd even odd even odd even odd
even odd even odd even       


Cheers,
Martin
-- 
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[
[[ Dr. Martin Schultz   Max-Planck-Institut fuer Meteorologie    [[
[[                      Bundesstr. 55, 20146 Hamburg             [[
[[                      phone: +49 40 41173-308                  [[
[[                      fax:   +49 40 41173-298                  [[
[[ martin.schultz@dkrz.de                                        [[
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[