Choose Language Hide Translation Bar
Highlighted
dbosch
Level II

Building table with JSL, issue with "New Column"

Hi,

 

I am trying to build a table in JSL from a data file using the file's first line of data to determine the data type.  However I am not able to create new columns in script with types other than Numeric.  This works:

dt << New Column("test", Character, Nominal)

However, this (simplified example) only generates Numeric columns:

cols = {"A", "B", "C"};
type = {};
For(i = 1, i <= N Items(cols), i = i + 1,
	type[i] = Match(cols[i], 
		"A", Expr(Character), 
		Expr(Numeric);
	);
);

For(i = 1, i <= N Items(cols), i = i + 1, 
	dt << New Column(cols[i], Eval(type[i]), Nominal);
);

The expected behavior would be to create one Character column and two Numerics.  What causes the expression type[i] to be ignored here?

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
txnelson
Super User

Re: Building table with JSL, issue with "New Column"

Some items within a New Column() function are not evaluated when parsed, and therefore require that they are complete JSL statements when processed.  The Substitute() function allows one to do that.  Here is the code that I would use to do what you are doing:

dt = New Table( "Example" );
cols = {"A", "B", "C"};
type = {};


For( i = 1, i <= N Items( cols ), i = i + 1,
	Eval(
		Substitute(
				Expr(
					dt << New Column( cols[i], __type__, Nominal )
				),
			Expr( __type__ ), Match( cols[i], "A", Expr( Character ), Expr( Numeric ) )
		)
	)
);
Jim

View solution in original post

2 REPLIES 2
Highlighted
txnelson
Super User

Re: Building table with JSL, issue with "New Column"

Some items within a New Column() function are not evaluated when parsed, and therefore require that they are complete JSL statements when processed.  The Substitute() function allows one to do that.  Here is the code that I would use to do what you are doing:

dt = New Table( "Example" );
cols = {"A", "B", "C"};
type = {};


For( i = 1, i <= N Items( cols ), i = i + 1,
	Eval(
		Substitute(
				Expr(
					dt << New Column( cols[i], __type__, Nominal )
				),
			Expr( __type__ ), Match( cols[i], "A", Expr( Character ), Expr( Numeric ) )
		)
	)
);
Jim

View solution in original post

Highlighted
dbosch
Level II

Re: Building table with JSL, issue with "New Column"

It works! Extending this you can do

Eval(
	Substitute(
		Expr(dt << New Column(__cols__, __type__, ...)),
		Expr(__cols__), cols[i],
		Expr(__type__), type[i],
		...
	);
);

Thanks!

Article Labels

    There are no labels assigned to this post.