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

issue in for loop

hello All, I am not able to figure out why this for loop formula is not working. It simply creating a new column if units are in time and then covert it into Hours. Sample data is also attached. Thanks.

ColNames = dt << get column names( string, Numeric, character, continuous, Nominal );
N Items( ColNames );
For( i = 1, i <= N Items( ColNames ), i++,
If( Ends With( ColNames[i], "(secs)" ),
New Column (Concat(ColNames[i], "_to_", "Hr"), Formula(AS Column(ColNames[i])/3600) ),
Ends With( ColNames[i], "(min)" ),
New Column (Concat(ColNames[i], "_to_", "Hr"), Formula(AS Column(ColNames[i])/60) ),)
);
Wait( 0 );




2 ACCEPTED SOLUTIONS

Accepted Solutions
txnelson
Super User

Re: issue in for loop

The issue is that the Formula element does not parse the formula presented and then place the resulting code into the formula.  Therefore, what is presented to the formula element, needs to be exactly the JSL the formula needs to be.  Below is a rework of your jsl

Names Default To Here( 1 );
dt = Current Data Table();
ColNames = dt << get column names( string, Numeric, character, continuous, Nominal );
//N Items( ColNames );
For( i = 1, i <= N Items( ColNames ), i++,
	If(
		Ends With( ColNames[i], "(secs)" ),
			New Column( Concat( ColNames[i], "_to_", "Hr" ),
				Formula( Eval( Parse( ":\!"" || ColNames[i] || "\!"n / 3600" ) ) )
			),
		Ends With( ColNames[i], "(min)" ),
			New Column( Concat( ColNames[i], "_to_", "Hr" ),
				Formula( Eval( Parse( ":\!"" || ColNames[i] || "\!"n / 60" ) ) )
			), 

	)
);
Jim

View solution in original post

ErraticAttack
Level VI

Re: issue in for loop

You might also consider using JSL Quote(), which allows the code to remain highlighted

 

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

For( i = 1, i <= N Cols( dt ), i++,
	column name = Column( dt, i ) << Get Name;
	If( Ends With( column name, "(secs)" ),
		Eval( Parse( Eval Insert( JSL Quote(
		dt << New Column( "^column name^_to_Hr", Formula( :Name("^column name^" ) / 3600 ) )
		) ) ) )
	,
		Ends With( column name, "(min)" ),
		Eval( Parse( Eval Insert( JSL Quote(
		dt << New Column( "^column name^_to_Hr", Formula( :Name("^column name^" ) / 60 ) )
		) ) ) )

	)
);
Jordan

View solution in original post

4 REPLIES 4
txnelson
Super User

Re: issue in for loop

The issue is that the Formula element does not parse the formula presented and then place the resulting code into the formula.  Therefore, what is presented to the formula element, needs to be exactly the JSL the formula needs to be.  Below is a rework of your jsl

Names Default To Here( 1 );
dt = Current Data Table();
ColNames = dt << get column names( string, Numeric, character, continuous, Nominal );
//N Items( ColNames );
For( i = 1, i <= N Items( ColNames ), i++,
	If(
		Ends With( ColNames[i], "(secs)" ),
			New Column( Concat( ColNames[i], "_to_", "Hr" ),
				Formula( Eval( Parse( ":\!"" || ColNames[i] || "\!"n / 3600" ) ) )
			),
		Ends With( ColNames[i], "(min)" ),
			New Column( Concat( ColNames[i], "_to_", "Hr" ),
				Formula( Eval( Parse( ":\!"" || ColNames[i] || "\!"n / 60" ) ) )
			), 

	)
);
Jim
HSS
HSS
Level IV

Re: issue in for loop

Thanks you Jim. it is working.

ErraticAttack
Level VI

Re: issue in for loop

You might also consider using JSL Quote(), which allows the code to remain highlighted

 

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

For( i = 1, i <= N Cols( dt ), i++,
	column name = Column( dt, i ) << Get Name;
	If( Ends With( column name, "(secs)" ),
		Eval( Parse( Eval Insert( JSL Quote(
		dt << New Column( "^column name^_to_Hr", Formula( :Name("^column name^" ) / 3600 ) )
		) ) ) )
	,
		Ends With( column name, "(min)" ),
		Eval( Parse( Eval Insert( JSL Quote(
		dt << New Column( "^column name^_to_Hr", Formula( :Name("^column name^" ) / 60 ) )
		) ) ) )

	)
);
Jordan
HSS
HSS
Level IV

Re: issue in for loop

Thanks Erratic, It is working. Both solutions (from Jim and yourself) working fine. But the solution you have provided - I never saw (JSL Code()). Thank you.