cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
JMPuser17
Level I

access column values in a formula in a loop

I want to write a script to create a new column for each column present in the original table; the new column values equal a constant value subtracted from the original column values. How do I access the values in the column?  The 'cnme' reads the column references accurately but Column(cnme) does not read the values in the column

 

Code:


a = N Col( Data Table( "vert" ) );

For( i = 2, i <= a, i++,
cnme = Column( i ) << get name;
New Column( "col", Formula(Subtract(Column(cnme), 6.5) ) ) );

1 REPLY 1
txnelson
Super User

Re: access column values in a formula in a loop

A column formula needs to be fully expanded into the full JSL statement, because otherwise if the values will change as the variables referenced in the formula is changed as additional columns are specified.  In your case, the variable cnme changes multiple times.  See below for a couple examples of how to work abound this.

Names Default To Here( 1 );

a = N Col( Data Table( "vert" ) );

For( i = 2, i <= a, i++,
	cnme = Column( i ) << get name;
	Eval(
		Substitute(
				Expr(
					New Column( "Adjusted " || cnme, Formula( Subtract( As Column( _cnme_ ), 6.5 ) ) )
				),
			Expr( _cnme_ ), cnme
		)
	);
);
Names Default To Here( 1 );

a = N Col( Data Table( "vert" ) );

For( i = 2, i <= a, i++,
	cnme = Column( i ) << get name;
	Eval( Eval Expr( New Column( "Adjusted " || cnme, Formula( Subtract( As Column( Expr( cnme ) ), 6.5 ) ) ) ) );
);
Jim