cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
JMP is taking Discovery online, April 16 and 18. Register today and join us for interactive sessions featuring popular presentation topics, networking, and discussions with the experts.
Choose Language Hide Translation Bar
ENTHU
Level IV

Case Insensitive searches using contains

I am using the following piece of code to check if the list cn contains "col_name" and if yes rename it as Target.The problem is col_name can be all upper case or lowercase and there is no way for me to control that.How do I make sure that the code gets executed irrespective of the case?

 

If( Contains( cn ,Lowercase("col_name") ),  


:"col_name" << Set name("Target");

);

 

Thanks

4 REPLIES 4
gianpaolo
Level IV

Re: Case Insensitive searches using contains

hello,

i think you can apply "substitute" ... follow the below example

  

Names Default To Here( 1 );

lst = {"a", "lowercase", "c"};

Substitute( lst, "lowercase", "Target" );

 

ciao

Gianpaolo

Gianpaolo Polsinelli
txnelson
Super User

Re: Case Insensitive searches using contains

What is "cn"? A List, a String, an Associative Array???
Is "col_name" supposed to be referring to the name of a column?

Please provide more specifics

Jim
pmroz
Super User

Re: Case Insensitive searches using contains

There's no need to use lowercase() on col_name because it's already in lowercase.  This code will do what you want:

dt = New Table( "Untitled 2", Add Rows( 1 ),
	New Column( "COL_Name", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [1] ) ),
	New Column( "Column 2", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [2] ) ),
	New Column( "Column 3", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [3] ) )
);
cn_list = dt << get column names(string);
for (i = 1, i <= nitems(cn_list), i++,
	cn_list[i] = lowercase(cn_list[i]);
);

If( Contains( cn_list, "col_name" ),
	:"col_name" << Set name("Target");
);
jay_holavarri
Level III

Re: Case Insensitive searches using contains

For the record, a couple times a year this case sensitivity with Contains() bugs me. It may be a personal problem, but I dislike having to add that For loop to make a list all upper or lower case for future use with Contains(). I wish Contains had a case sensitivity option.

 

My preferred method is this to force the case for all items in a list:

 

myList = {"Steelers", "Falcons", "rams", "SeaHawks"};
myNewList = Words(Upper Case(Concat Items(myList)));

 

Cheers