Subscribe Bookmark RSS Feed

Can I use quote marks on column names in a formula?

Radko

Community Member

Joined:

Jul 10, 2017

Hi,

 

I have the following simplified script in a table:

 

dataTable = Current Data Table();
If( !Contains( strColNames, "pears" ),
    dataTable << New Column( "pears",
        numeric,
        "Continuous",
        Formula( :"apples" + 2 )
    )
);

When I copy this script to a new table (using ctrl-c ctrl-v), which already has a filled column named "apples", the script ends up as:

dataTable = Current Data Table();
If( !Contains( strColNames, "pears" ),
	dataTable << New Column( "pears",
		numeric,
		"Continuous",
		Formula( As Column() + 2 )
	)
);

The reference to the column "apples" is replaced in the formula by the As Column() statement. The script no longer works. It turns out that removing the quotation marks from the reference to the column "apples" in the formula solved the problem.

I consider this a bug. Is it, or is this expected behaviour?

 

Regards, Radko

 

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson

Super User

Joined:

Jun 22, 2012

Solution

You will need to manually handle the quotes.  See the code below:

dataTable = Current Data Table();
If( !Contains( strColNames, "\!"pears\!"" ),
	dataTable << New Column( "\!"pears\!"",
		numeric,
		"Continuous",
		Formula( :Name( "\!"apples\!"" ) + 2 )
	)
);
Jim
2 REPLIES
txnelson

Super User

Joined:

Jun 22, 2012

Solution

You will need to manually handle the quotes.  See the code below:

dataTable = Current Data Table();
If( !Contains( strColNames, "\!"pears\!"" ),
	dataTable << New Column( "\!"pears\!"",
		numeric,
		"Continuous",
		Formula( :Name( "\!"apples\!"" ) + 2 )
	)
);
Jim
vt_sailor

Occasional Contributor

Joined:

May 11, 2017

I do this slightly differently.  I use the function below to create the column.  If the column exists it simply returns the column handle, if the column does not exist it creates a new column and returns the handle.  So the usage would be something like this:

 

PearCol=createcol(dt,"Pears","NUMERIC");   // "NUMERIC" or "CHARACTER"

AppleCol=createcol(dt,"Apples","NUMERIC");

PearCol<<set each value(AppleCol[]+2);

 

I prefer to use 'set each value' but you can also set a formula if that makes more sense in your application.

 

 

 

CreateCol = Function( {ct, ccn, ColType},
{default local},
CreateColExpr = Expr(
stuff = __CurDataTable__ << New Column( __ColName__, __ColType__ )
);
stuff = Try( Column( ct, ccn ), -1 );
Wait( 0 );
If( stuff == -1,
CreateColumn = Substitute( Name Expr( CreateColExpr ),
Expr( __ColType__ ), Parse( ColType ),
Expr( __CurDataTable__ ), Eval( ct ),
Expr( __ColName__ ), Eval( ccn )
);
Show( CreateColumn );
Eval( CreateColumn );
);
stuff;
);