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
AT
AT
Level V

Select column name using contains function

Hi,

I like to select multiple columns that have names contains a string. I tried below script and no error but the columns does not get selected.

 

 

col_list = dt3 << get column names(string);
for( i = 1, i <= N Items( col_list ), i++,
If(Contains( col_list[i], "A_B_C" ),
Column(col_list[i] ) << Set Selected (1)
)
);

 

 

When I check col_list, it has all the colums in entire data table. I appreciate your help. Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: Select column name using contains function

The Contains() function takes the 2nd argument and looks into the 1st argument to see if the 2nd argument's value can be found in the first argument.  Therefore, if the Contains() function is

     Contains("A_B_C", Col_List[i])

and the value of Col_List[i] = "A_B_C" it will take the value "A_B_C" and look into the first arguement to see if it can be found.  Which it can, because the first arguement contains "A_B_C".  Now if the column name is "A", and you do the same comparison, the Contains() function will take the 2nd arguement, "A" and look into the 1st arguement, "A_B_C" and it will find that an "A" is in the first arguement, and therefore it will be a true comparison.

 

Thus, the original form of the Contains() function is what is wanted

Names Default To Here( 1 );
dt = Current Data Table();

Col_List = dt << Get Column Names("String");

for(i = 1, i <= N Items(Col_List),i++,
If(Contains(Col_List[i], "A_B_C"),
Column(Col_List[i]) << Set Selected(1);
);
);
Jim

View solution in original post

5 REPLIES 5
uday_guntupalli
Level VIII

Re: Select column name using contains function

@AT
     I think you have the Contains() arguments inverted. 

 

dt = Current Data Table(); 

Col_List = dt << Get Column Names("String"); 

for(i = 1, i <= N Items(Col_List),i++,
		If(Contains("A_B_C",Col_List[i]),
			Column(Col_List[i]) << Set Selected(1);
		  );
   );

image.png

Best
Uday
AT
AT
Level V

Re: Select column name using contains function

Thanks. I tried your script but with differer example and I still have an issue.

 

Names Default To Here( 1 );
dt = Current Data Table();

Col_List = dt << Get Column Names("String");

for(i = 1, i <= N Items(Col_List),i++,
If(Contains("A_B_C",Col_List[i]),
Column(Col_List[i]) << Set Selected(1);
);
);

 

The columns A_B_C and A is selcted. I don't understand why. I appreciate your help. I have attached the image result.

Byron_JMP
Staff

Re: Select column name using contains function

there might be a couple of things going on.

One posibility is that a column is selected before the script runs?

Try adding this to the beginning of your script, right after dt is defined.

dt << Clear Selection;
JMP Systems Engineer, Health and Life Sciences (Pharma)
txnelson
Super User

Re: Select column name using contains function

The Contains() function takes the 2nd argument and looks into the 1st argument to see if the 2nd argument's value can be found in the first argument.  Therefore, if the Contains() function is

     Contains("A_B_C", Col_List[i])

and the value of Col_List[i] = "A_B_C" it will take the value "A_B_C" and look into the first arguement to see if it can be found.  Which it can, because the first arguement contains "A_B_C".  Now if the column name is "A", and you do the same comparison, the Contains() function will take the 2nd arguement, "A" and look into the 1st arguement, "A_B_C" and it will find that an "A" is in the first arguement, and therefore it will be a true comparison.

 

Thus, the original form of the Contains() function is what is wanted

Names Default To Here( 1 );
dt = Current Data Table();

Col_List = dt << Get Column Names("String");

for(i = 1, i <= N Items(Col_List),i++,
If(Contains(Col_List[i], "A_B_C"),
Column(Col_List[i]) << Set Selected(1);
);
);
Jim
AT
AT
Level V

Re: Select column name using contains function

Thanks Jim for your help. It works well now.