BookmarkSubscribeRSS Feed
Highlighted
KfirDarina

New Contributor

Joined:

Dec 4, 2018

Create local variables that hold lists from column names

Hi all, 

Need your help with creating multiple local variables that contain column names within var name.

I'v tried this butget an err.

Any suggestions?


for(j=1,j<=ncol(dt),j++,
local(parse(char(column name(j))||"key")=Associative Array(column(j)<<Get Values)<<get keys));

1 ACCEPTED SOLUTION

Accepted Solutions
ian_jmp

Staff

Joined:

Jun 23, 2011

Solution

Re: Create local variables that hold lists from column names

OK. Here's one way, and I'm sure there will be others:

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );

varList = {};
For( j = 1, j <= N Col( dt ), j++,
	varName = (Column(dt, j ) << getName)||"_keys";
	keys = Associative Array( Column(dt, j ) << Get Values ) << get keys;
	makeVar = Expr(LHS = RHS);
	SubstituteInto(makeVar, Expr(LHS), Parse(varName), Expr(RHS), Eval(keys));
	makeVar;
	InsertInto(varList, Parse(varName));
);
Print(varList);
Print(EvalList(varList));
4 REPLIES
ian_jmp

Staff

Joined:

Jun 23, 2011

Re: Create local variables that hold lists from column names

Not sure I understand your use of 'Local()', but is this close to what you want?

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );

keyVarList = {};
For( j = 1, j <= N Col( dt ), j++,
	keys = Associative Array( Column( j ) << Get Values ) << get keys;
	InsertInto(keyVarList, EvalList(List(keys)));
);

for(j = 1, j <= NItems(keyVarList), j++,
	Print(keyVarList[j]);
);
KfirDarina

New Contributor

Joined:

Dec 4, 2018

Re: Create local variables that hold lists from column names

ian thx,

My intention is that for each list that was created by associative aray function it will also have a unique variable name that contain the matched column name. 

For example: if I have two columns: Entity and Lot, then I will get two local variables :

 

Entitykey={entity1, entity2}

Lotkey={Lot1,Lot2}....

And so on for all columns in table

ian_jmp

Staff

Joined:

Jun 23, 2011

Solution

Re: Create local variables that hold lists from column names

OK. Here's one way, and I'm sure there will be others:

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );

varList = {};
For( j = 1, j <= N Col( dt ), j++,
	varName = (Column(dt, j ) << getName)||"_keys";
	keys = Associative Array( Column(dt, j ) << Get Values ) << get keys;
	makeVar = Expr(LHS = RHS);
	SubstituteInto(makeVar, Expr(LHS), Parse(varName), Expr(RHS), Eval(keys));
	makeVar;
	InsertInto(varList, Parse(varName));
);
Print(varList);
Print(EvalList(varList));
KfirDarina

New Contributor

Joined:

Dec 4, 2018

Re: Create local variables that hold lists from column names

ian thx. this is great, i was battling for 2 days to solve it, appreciate that :)