Subscribe Bookmark RSS Feed

How to use column name as a variable.

qt

Community Trekker

Joined:

Oct 17, 2012

I want to select some rows from a column ( I have a column named as 'RESOLUTION' in the current data table)

This two lines will work:

======================================

dt = Current Data Table();

dt<<Select Where(RESOLUTION > 0.);

======================================

If I define 'RESOLUTION' as another variable, it doesn't work:

============================================

dt = Current Data Table();

colA="RESOLUTION";

dt<<Select Where(column(dt, colA) > 0.);

============================================

This will not select any row.

Could someone kindly tell me how could I do this?

Thanks.

3 REPLIES
pmroz

Super User

Joined:

Jun 23, 2011

qt,

This will do the trick.

dt = Current Data Table();

colA = "Resolution";

col_expr = "dt << Select Where(:name(\!"" || colA || "\!") > 0.)";

eval(parse(col_expr));

Basically you're creating some dynamic code, and then executing it.

qt

Community Trekker

Joined:

Oct 17, 2012

It works!!

Many thanks!

David_Burnham

Super User

Joined:

Jul 13, 2011

Alternatively use the AsColumn function:

colA="RESOLUTION";

dt<<Select Where(AsColumn(colA) > 0.)

-Dave