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

How to replace an entire column with new content, if it exists

This post is a follow-up and extension of the solved forum here

 

The following script was developed in that post: 

 

Names Default To Here(1);

dt = Current Data Table();

//get the unique character variable list
col9 = Column(dt, "Column 1");
unique_char = Associative Array(col9) << get keys;
nuniquechar = N Items(unique_char); //will be 5, if categories are A, B, C, D, E
unique_aa = Associative Array(unique_char, Repeat({.}, nuniquechar)); //repeat to initialize values as missing

//create new window to create multiple number edit boxes
vlbExpr = V List Box();
For(i = 1, i <= nuniquechar, i++,
	Insert Into(vlbExpr, Text Box("Number for " || Char(unique_char[i]) || ":"));   //i.e. "Number for A:"
	Eval(
		Eval Expr(Insert Into(vlbExpr, Number Edit Box(., <<Set Function(Function({this}, unique_aa[Expr(unique_char[i])] = this << get)))))
	);
);

nw = New Window("Set Values",
	<<Modal,
	vlbExpr,
	H List Box(
		Button Box("OK", 
		
		),
		Button Box("Cancel")
	)
);
//Show(unique_aa);

Eval(EvalExpr(dt << New Column("Column 2",
	Numeric,
	Continuous,
	<< Formula(
		Expr(unique_aa)[:Column 1]
	);
)));

If Column 2 already exists, and the user wants to replace it with a set of new values, by re-running the script and entering in a new set of values. 

 

I tried to use the update function. If Column 2 exists, the update function will be used on Column 2. Else, Column 2 will be created. as above. but I am not familiar with the Eval expression and need a better understanding of where I could put a statement like this in the script if it works out.  

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: How to replace an entire column with new content, if it exists

Here is one method that will handle the issue where the column already exists

Names Default To Here( 1 );

dt = Current Data Table();

//get the unique character variable list
col9 = Column( dt, "Column 1" );
unique_char = Associative Array( col9 ) << get keys;
nuniquechar = N Items( unique_char ); //will be 5, if categories are A, B, C, D, E
unique_aa = Associative Array( unique_char, Repeat( {.}, nuniquechar ) ); //repeat to initialize values as missing

//create new window to create multiple number edit boxes
vlbExpr = V List Box();
For( i = 1, i <= nuniquechar, i++,
	Insert Into( vlbExpr, Text Box( "Number for " || Char( unique_char[i] ) || ":" ) );   //i.e. "Number for A:"
	Eval(
		Eval Expr(
			Insert Into(
				vlbExpr,
				Number Edit Box(
					.,
					<<Set Function(
						Function( {this},
							unique_aa[Expr( unique_char[i] )] = this << get
						)
					)
				)
			)
		)
	);
);

nw = New Window( "Set Values",
	<<Modal,
	vlbExpr,
	H List Box(
		Button Box( "OK", 
		
		),
		Button Box( "Cancel" )
	)
);
//Show(unique_aa);

If( Try( dt:Column 2 << get name, "" ) != "",
	Eval( Eval Expr( dt:Column 2 << set formula( Expr( unique_aa )[:Column 1] ) ) ),
	Eval(
		Eval Expr(
			dt << New Column( "Column 2",
				Numeric,
				Continuous,
				<<Formula( Expr( unique_aa )[:Column 1] )
			)
		)
	)
);
Jim

View solution in original post

1 REPLY 1
txnelson
Super User

Re: How to replace an entire column with new content, if it exists

Here is one method that will handle the issue where the column already exists

Names Default To Here( 1 );

dt = Current Data Table();

//get the unique character variable list
col9 = Column( dt, "Column 1" );
unique_char = Associative Array( col9 ) << get keys;
nuniquechar = N Items( unique_char ); //will be 5, if categories are A, B, C, D, E
unique_aa = Associative Array( unique_char, Repeat( {.}, nuniquechar ) ); //repeat to initialize values as missing

//create new window to create multiple number edit boxes
vlbExpr = V List Box();
For( i = 1, i <= nuniquechar, i++,
	Insert Into( vlbExpr, Text Box( "Number for " || Char( unique_char[i] ) || ":" ) );   //i.e. "Number for A:"
	Eval(
		Eval Expr(
			Insert Into(
				vlbExpr,
				Number Edit Box(
					.,
					<<Set Function(
						Function( {this},
							unique_aa[Expr( unique_char[i] )] = this << get
						)
					)
				)
			)
		)
	);
);

nw = New Window( "Set Values",
	<<Modal,
	vlbExpr,
	H List Box(
		Button Box( "OK", 
		
		),
		Button Box( "Cancel" )
	)
);
//Show(unique_aa);

If( Try( dt:Column 2 << get name, "" ) != "",
	Eval( Eval Expr( dt:Column 2 << set formula( Expr( unique_aa )[:Column 1] ) ) ),
	Eval(
		Eval Expr(
			dt << New Column( "Column 2",
				Numeric,
				Continuous,
				<<Formula( Expr( unique_aa )[:Column 1] )
			)
		)
	)
);
Jim