BookmarkSubscribe
Choose Language Hide Translation Bar
cjohnson6
Community Trekker

Get Column scripts using For Loop

I have a data table with a bunch of new columns based on formulas I laboriously entered manually. I want to create a JSL script that will allow me to recreate all these columns in another table.

 

The following script will return what I want for a single column:

 

dt=current data table();
column(dt,1) << Get script; 

 

 

I wanted to automate it using a FOR loop, but can't get it to work. Any suggestions on how to fix this would be greatly appreciated!

 

Here is what I tried:

 

dt=current data table();
j= N cols(dt);
For( i = 1, i <=j , i++,
Column(dt,i) << Get Script;
 );

 

 

 

0 Kudos
4 REPLIES 4
txnelson
Super User

Re: Get Column scripts using For Loop

You can get the script for the entire data table at once

dt = Current Data Table();
myScript = dt << get script;

It's drawback is that it will bring all of the data embedded in the script.  So the easy thing to do, is to delete the data first, and then retrieve the script

dt = Current Data Table();
dt << select all rows << delete rows;
myScript = dt << get script;

Then it becomes a simple matter of editing the retrieved script.

Concerning your script, what I assume you are looking to do is to display the results of your << get script in the log, so you can cut and paste from there.  JMP typically displays the last results of the last statement when running a script.  Therefore, when you run just  one column <<get script, the results are displayed.  If you want to do it in a loop, you would need to force the results to the log using a Write, Print, or Show function.

dt = Current Data Table();
j = N Cols( dt );
For( i = 1, i <= j, i++,
	Show( Column( dt, i ) << Get Script )
);

Or, if you were looking to retain the values into a structure you could programmatically work with, you could place the results into a list

dt = Current Data Table();
myList = {};
j = N Cols( dt );
For( i = 1, i <= j, i++,
	myList[i] = Column( dt, i ) << Get Script
);
Jim
Jeff_Perkinson
Community Manager Community Manager

Re: Get Column scripts using For Loop

I just uploaded a Copy Formulas add-in to the File Exchange.

 

It will give you an interactive way to do this from the Add-ins menu.

 

JMPScreenSnapz190.png

No need to write your own script.

-Jeff
Jeff_Perkinson
Community Manager Community Manager

Re: Get Column scripts using For Loop

For reference, the business part of the add-in script is this:

 

 

//collist is a list of the formula columns to be copied
For( i = 1, i <= N Items( collist ), i++,

//create a new column in the copy to data table
//substitute replaces the form in the New Column() expression with the formula from
//the source column
				Eval(
					Substitute(
							Expr(
								dt2 <<
								New Column(
									Column(
										chosen,
										Char( collist[i] )
									) << get name,
									formula( form )
								)
							),
						Expr( form ),
							Column( chosen, Char( collist[i] ) )
							 << get formula
					)
				)
			);

 

 

-Jeff
julian
Staff

Re: Get Column scripts using For Loop

Hi @cjohnson6,

The Copy Script for Selected Columns Add-In will do just this -- select your formula column, run the Add-In, and the combined script for all selected columns will be copied to your clipboard so you can paste it into your JSL workflow.

@julian