cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
Choose Language Hide Translation Bar
Neo
Neo
Level VI

How to use use 'not equal to' with logical or

In an if statement I would like to do if myParameter is not equal to either Parameter1 or Parameter2 then return something.  But my following function is not working as expected.,. Where am I going wrong

Names Default To Here(1);
myFunc =  function ({myParameter}, {default local},
    If (myParameter != Or("Parameter1","Parameter2"), 
        return(Eval Insert("output some msg")),
    );
);
clear log ();
//output = myFunc ("Parameter1");
output = myFunc ("notParameter1");

 

When it's too good to be true, it's neither
1 ACCEPTED SOLUTION

Accepted Solutions
jthi
Super User

Re: How to use use 'not equal to' with logical or

If you want to use Or the comparison will get very long and annoying to maintain if the allowed parameter list increases. So I would use !Contains and list of allowed parameters (or associative array):

Names Default To Here(1);

myFunc = Function({myParameter},
	{default local},
	ok_parameters = {"Parameter1", "Parameter2"};
	If(!Contains(ok_parameters, myParameter),
		Return(Eval Insert("^myParameter^ not in list ^ok_parameters^"));
	,
		Return(Eval Insert("^myParameter^ in list ^ok_parameters^"));
	);
);

output = myFunc("Parameter1");
Show(output);
output = myFunc("notParameter1");
Show(output);

But you can use or/and also, below is one example (| means or):

Names Default To Here(1);

myFunc = Function({myParameter},
	{default local},
	If(!(myParameter == "Parameter1" | myParameter == "Parameter2"),
        return(Eval Insert("output some msg"));
	);
);

output = myFunc("Parameter1");
show(output);
output = myFunc ("notParameter1");
show(output);
-Jarmo

View solution in original post

3 REPLIES 3
Craige_Hales
Super User

Re: How to use use 'not equal to' with logical or

You probably mean this (which I like the best)

if ( (myParameter != Parameter1) & (myParameter != Parameter2) , ...

which is the same as this

if( ! ( (myParameter == Parameter1) | (myParameter == Parameter2) ), ...

or, the way you were using the or() function, the same as this

if( !or(myParameter==Parameter1, myParameter==Parameter2), ...

https://en.wikipedia.org/wiki/De_Morgan%27s_laws

 

Also, if there are missing values, be sure to handle them with isMissing() because they don't compare like you might expect.

Craige
jthi
Super User

Re: How to use use 'not equal to' with logical or

If you want to use Or the comparison will get very long and annoying to maintain if the allowed parameter list increases. So I would use !Contains and list of allowed parameters (or associative array):

Names Default To Here(1);

myFunc = Function({myParameter},
	{default local},
	ok_parameters = {"Parameter1", "Parameter2"};
	If(!Contains(ok_parameters, myParameter),
		Return(Eval Insert("^myParameter^ not in list ^ok_parameters^"));
	,
		Return(Eval Insert("^myParameter^ in list ^ok_parameters^"));
	);
);

output = myFunc("Parameter1");
Show(output);
output = myFunc("notParameter1");
Show(output);

But you can use or/and also, below is one example (| means or):

Names Default To Here(1);

myFunc = Function({myParameter},
	{default local},
	If(!(myParameter == "Parameter1" | myParameter == "Parameter2"),
        return(Eval Insert("output some msg"));
	);
);

output = myFunc("Parameter1");
show(output);
output = myFunc ("notParameter1");
show(output);
-Jarmo
SDF1
Super User

Re: How to use use 'not equal to' with logical or

Hi @Neo ,

 

  I think the problem might be in how your calling the "or" logical operator. In JSL, the logical or is this character | . So, you'd change the If() statement to something like the following it should work:

Names Default To Here( 1 );

MyFunc = Function( {Myparam},
	If(
		(Myparam != parm1 | Myparam != parm2), Return( Eval Insert( "Output some msg" ) )
	)
);

Keep in mind the logical only requires one of those two to be true and it will return the result. If Myparam equals one of those, then it still will return the message.

 

Hope this helps!,

DS