|
Re: OR [message #70728 is a reply to message #67037] |
Mon, 03 May 2010 16:06  |
jeanh
Messages: 79 Registered: November 2009
|
Member |
|
|
On 03/05/2010 5:55 PM, fgg wrote:
> Hello there,
>
> How can I summarize these two lines in just one line of code?
>
> if (line[0] eq 'i_rng_wf' and n_elements(line) ne 4002) then message,
> $
> 'The # of samples per shot is not valid.'
> if (line[0] eq 'i_rng_wf' and n_elements(line) ne 10882) then message,
> $
> 'The # of samples per shot is not valid.'
>
> ...I thought using OR would do the trick, but I guess I'm missing
> something here:
>
> if (line[0] eq 'i_rng_wf' and n_elements(line) ne (4002 or 10882))
> then message, ...
>
> Thanks
Don't you want to use "and" here (on top of what Mike Galloy said to
have the "or" working)?
If n_elements(line) IS 4002, the the other condition (10882) will be
caught!... in other words, you will always have your error message.
a = 8
if (a ne 1 or a ne 8) then print, "error" ===>error
if (a ne 1 and a ne 8) then print ,"error" ===> nothing.
a=100
if (a ne 1 or a ne 8) then print, "error" ===>error
if (a ne 1 and a ne 8) then print ,"error" ===> error.
Jean
|
|
|
Re: OR [message #70730 is a reply to message #67037] |
Mon, 03 May 2010 15:04  |
Michael Galloy
Messages: 1114 Registered: April 2006
|
Senior Member |
|
|
On 5/3/10 3:55 PM, fgg wrote:
> Hello there,
>
> How can I summarize these two lines in just one line of code?
>
> if (line[0] eq 'i_rng_wf' and n_elements(line) ne 4002) then message,
> $
> 'The # of samples per shot is not valid.'
> if (line[0] eq 'i_rng_wf' and n_elements(line) ne 10882) then message,
> $
> 'The # of samples per shot is not valid.'
>
> ...I thought using OR would do the trick, but I guess I'm missing
> something here:
>
> if (line[0] eq 'i_rng_wf' and n_elements(line) ne (4002 or 10882))
> then message, ...
>
> Thanks
Using the bitwise operators, it would be:
if (line[0] eq 'i_rng_wf' and (n_elements(line) ne 4002 or
n_elements(line) ne 10882) then ...
I would consider using logical operators if you only have to be IDL 6.0+
compatible:
if (line[0] eq 'i_rng_wf' && (n_elements(line) ne 4002 ||
n_elements(line) ne 10882) then ...
The logical operators will short-circuit.
-Mike
--
www.michaelgalloy.com
Research Mathematician
Tech-X Corporation
|
|
|