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

Get Column Names from List of Columns

 

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

column list = dt << Get Column Names(Continuous);
key list = dt << Get Column Group("KEYS");

"column list" contains a list of names from columns containing continuous data.

"key list" contains a list of columns in the "KEYS" group. 

 

How do I get the names of the columns from "key list"? Ultimately I would like to remove any continuous column names from the "column list" that are in the "KEYS" group. I found a post from Craige Hales to manage the lists, but I need to get them in the same format.

 

1 ACCEPTED SOLUTION

Accepted Solutions
MarkDayton
Level IV

Re: Get Column Names from List of Columns

Here is the solution I arrived at after converting the key list to strings as suggested:

 

// Initialize
Names Default to Here(1);
dt = Current Data Table();
key list names  = {};

// Get a list of all continuous columns
column list = dt << Get Column Names(Continuous, String);

// Create a list of column names in the KEYS group
key list = dt << Get Column Group("KEYS");
For(i = 1, i <= N Items(key list), i++,
	Insert Into(key list names, Char(key list[i]))
); 

// Remove from the column list any column that is in the KEYS group
/* 
Adapted from - Re: Quick way to compare two lists (uncommon elements)? JAN 10, 2017 6:41 AM, by Craige_Hales, STAFF (RETIRED) */ set1 = Associative Array(column list); set2 = Associative Array(key list names); // Find the names common to both lists intersection = set1; intersection << Intersect(set2); // Remove the common names cleaned list = set1; cleaned list << Remove(intersection); column list = cleaned list << Get Keys;

View solution in original post

7 REPLIES 7
txnelson
Super User

Re: Get Column Names from List of Columns

What is returned from both the << Get Column Names, and << Get Column Group are JMP lists.  You can roughly think of them as Arrays of values.  Please review the Scripting Guides section on using lists.

Below is a modification of your code, that will check for the comparison between 2 lists, for column names found common in both lists and then removing them from the Key List, list.  Please note, that the Contains function requires literal strings for it to function, so that is why the <<get column names has the "string" option added to it, and that the Contains() function has the search element from the Keys List, converted to a character string before the search across the Column Names list.

Names Default To Here( 1 );
dt = Open( "$SAMPLE_DATA/semiconductor capability.jmp" );

column list = dt << Get Column Names( Continuous, string );
key list = dt << Get Column Group( "Processes" );
Show( "complete list", key list );

For( i = N Items( key list ), i >= 1, i--,
	If( Contains( column list, Char( key list[i] ) ),
		key list = Remove( key list, i, 1 )
	)
);
Show( "final list", key list );

In my example, the Keys List ends up not having any values, since all elements in the group are numeric columns

Jim
MarkDayton
Level IV

Re: Get Column Names from List of Columns

Thanks. I am looking to remove from "column list" any name that is in the "Processes" group (using your example), so items need to be removed from "column list", not "key list". I expect that I will need to search column list for Char(key list[i]) and if found remove it.
MarkDayton
Level IV

Re: Get Column Names from List of Columns

Here is the solution I arrived at after converting the key list to strings as suggested:

 

// Initialize
Names Default to Here(1);
dt = Current Data Table();
key list names  = {};

// Get a list of all continuous columns
column list = dt << Get Column Names(Continuous, String);

// Create a list of column names in the KEYS group
key list = dt << Get Column Group("KEYS");
For(i = 1, i <= N Items(key list), i++,
	Insert Into(key list names, Char(key list[i]))
); 

// Remove from the column list any column that is in the KEYS group
/* 
Adapted from - Re: Quick way to compare two lists (uncommon elements)? JAN 10, 2017 6:41 AM, by Craige_Hales, STAFF (RETIRED) */ set1 = Associative Array(column list); set2 = Associative Array(key list names); // Find the names common to both lists intersection = set1; intersection << Intersect(set2); // Remove the common names cleaned list = set1; cleaned list << Remove(intersection); column list = cleaned list << Get Keys;
MarkDayton
Level IV

Re: Get Column Names from List of Columns

The trick was: Char(key list[i]))
(I never thought of casting the column reference into a string)
TomF
Level II

Re: Get Column Names from List of Columns

Thanks for your note about contain requiring literal strings and the "get column names" not providing that with out the "string" option. I banged my head against this for a long time and adding "string" immediately fixed it.

Re: Get Column Names from List of Columns

Please do not bang your head! Use Help > Scripting Index instead.

 

get col names.PNG

Devin
Level I

Re: Get Column Names from List of Columns

Here's an easy task you can help me with - once I "get column names", how can I then insert them into a data table which is just that selfsame list of column names?   THANKS  Devin