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
ENTHU
Level IV

Help with contains within if else

Hi,

I have written the following piece of code which checks if/not a list contains a character and performs some steps accordingly.

For some reason, the script doesnt check for "if contains" condition and always executes the else part.I think I have a silly mistake here.

 

Clear Symbols();
Delete Symbols();
clear globals();
 
 

dtNext = Open("C:\temp\output.csv","invisible");
Summarize(dtNext, testlist = By(:test));
show(testlist );
SAVEPATH = "C:\temp\";
If(contains(testlist ,"M"),

Macrodt=New Table("test_type",
New Column("test_type",
Character,
Nominal,
Set Values( {"Morning"})
),
invisible
);




, //else
Macrodt=New Table("test_type",
New Column("test_type",
Character,
Nominal,
Set Values( {"Evening"})
),
invisible
);
);

//Save macro file
Macrodt<<Save(SAVEPATH||"test_type.csv");

 

1 ACCEPTED SOLUTION

Accepted Solutions
jerry_cooper
Staff (Retired)

Re: Help with contains within if else

As @Jeff_Perkinson suggests, more info would help, but I'll take a stab at what I think may be a source of confusion for you. The "Contains" function can be used on a list OR a string... if you are looking for whether any list item contains a specific character, your "If" statement will not produce the desired result as written. If that is your intent, you could surround your "testlist" variable with the Concat Items function in the "If" statement, i.e. Concat Items(testlist) evaluates to a single string made up of all of the list items stitched together. Now the Contains function will return a positive response when looking for a specific character if it exists in the new string. 

 

Hope this helps.

View solution in original post

3 REPLIES 3
Jeff_Perkinson
Community Manager Community Manager

Re: Help with contains within if else

There's no obvious mistake in your code. We'll need some more details to help figure where the confusion is.

 

Here's an example showing that your prototype is correct.

 

 

dts = {"$SAMPLE_DATA\Fitness.jmp", "$SAMPLE_DATA\Big Class.jmp"}; 
 
For( i = 1, i <= N Items( dts ), i++,
	dtNext = Open( dts[i] );
	Summarize( dtNext, testlist = By( :age ) );

	Show( testlist );

	If( Contains( testlist, "42" ), 

		Show( "testlist contains 42" );
	
	, //else
 
		Show( "testlist doesn't contain 42" )
	);
);

 

The log:

/*:

testlist = {"38", "40", "42", "43", "44", "45", "47", "48", "49", "50", "51", "52", "54", "57"};
"testlist contains 42";
testlist = {"12", "13", "14", "15", "16", "17"};
"testlist doesn't contain 42";
-Jeff
jerry_cooper
Staff (Retired)

Re: Help with contains within if else

As @Jeff_Perkinson suggests, more info would help, but I'll take a stab at what I think may be a source of confusion for you. The "Contains" function can be used on a list OR a string... if you are looking for whether any list item contains a specific character, your "If" statement will not produce the desired result as written. If that is your intent, you could surround your "testlist" variable with the Concat Items function in the "If" statement, i.e. Concat Items(testlist) evaluates to a single string made up of all of the list items stitched together. Now the Contains function will return a positive response when looking for a specific character if it exists in the new string. 

 

Hope this helps.

ENTHU
Level IV

Re: Help with contains within if else

I used cocncatitems function and it worked.
Thanks