cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Submit your abstract to the call for content for Discovery Summit Americas by April 23. Selected abstracts will be presented at Discovery Summit, Oct. 21- 24.
Discovery is online this week, April 16 and 18. Join us for these exciting interactive sessions.
Choose Language Hide Translation Bar
john_madden
Level VI

Text to Columns result columns

I'm using the built-in JSL Text to Columns() function. I won't know how many "segments" my input text has, so I won't know in advance how many new columns will result from calling the function. But I need to know this so I can do certain subsequent steps in my script.

 

Back when this was an add-in, Jeff Perkinson's v3 returned a result that gave the names of the columns that had been created; the version that is built in doesn't seem to return anything (?). I installed his v3, but I'm not getting scriptable access to his version of the function. (I'm on JMP 15 EA right now; I haven't diddled around other than trying to call the function in his namespace as described in the v3 docs).

 

Questions:

1. Assuming I'll use the built-in version of Text to Columns(), what's a good way to get the names of the columns that were created (or at least the *number* of columns that were created).

2. Would it be better to use J.P.'s v3, and if so, how do I make it callable these days.

 

John

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: Text to Columns result columns

Here is a simple way to get the number of added columns

Names Default To Here( 1 );
dt = New Table( "Example",
	Add Rows( 2 ),
	New Column( "the text", Character, "Nominal", Set Values( {"A,B,C,D", "X,Y,Z"} ) )
);

nColNow = N Items( dt << get column names );
dt << text to columns( delimiter( "," ), columns( :the text ) );
colAdded = N Items( dt << get column names ) - nColNow;
Jim

View solution in original post

3 REPLIES 3
txnelson
Super User

Re: Text to Columns result columns

Here is a simple way to get the number of added columns

Names Default To Here( 1 );
dt = New Table( "Example",
	Add Rows( 2 ),
	New Column( "the text", Character, "Nominal", Set Values( {"A,B,C,D", "X,Y,Z"} ) )
);

nColNow = N Items( dt << get column names );
dt << text to columns( delimiter( "," ), columns( :the text ) );
colAdded = N Items( dt << get column names ) - nColNow;
Jim
john_madden
Level VI

Re: Text to Columns result columns

I think I might just wrap the built-in Text to Columns() into a user function that adds this functionality in the form of a return value.
John
john_madden
Level VI

Re: Text to Columns result columns

This works well for me:

 

Add Custom Functions(
	New Custom Function(
		"my",
		"Text to Columns",
		// Wraps native Text to Columns() to make it return an associative array,
		// containing the split column name(s) split (as keys), and the number of 
		// columns added (as values)
		Function( {columns, delims, table = Current Data Table()},
			// First parameter should be a list, but accept single value without brackets
			If( Not( Is List( columns ) ),
				columns = {columns}
			);
			columns = table << Get Column Reference( columns );
			If( Not( Is String( delims ) ),
				Throw()
			);
			n_cols = N Items( columns );
			result = Associative Array();
			For( i = 1, i <= n_cols, i++,
				cols_before = N Cols( table );
				Eval( Parse( Eval Insert( "^table^ << Text to Columns(delimiters(\!"^delims^\!"), columns(^columns[i]^))" ) ) );
				cols_added = N Cols( table ) - cols_before;
				result << Insert Item( columns[i] << Get Name, cols_added );
			);
			Return( result);
		)
	)
);

Example:

Names Default to Here(1);
dt = Open( "$SAMPLE_DATA/Consumer Preferences.jmp" );
my:Text to Columns({:Brush Delimited, :Floss Delimited}, ",", dt)

["Brush Delimited" => 4, "Floss Delimited" => 4]