BookmarkSubscribe
Choose Language Hide Translation Bar
KfirDarina
Occasional Contributor

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));

0 Kudos
1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
ian_jmp
Staff

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 4
ian_jmp
Staff

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]);
);
0 Kudos
KfirDarina
Occasional Contributor

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

0 Kudos
Highlighted
ian_jmp
Staff

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
Occasional Contributor

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 :)
0 Kudos