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

take each item in list become an empty list using for loop

Hi ,

How I can create variables name and assign it as empty list using for loop? 

 

for example:
var_list = {"day",  "today" ,"rain", "mean"};

 

loop through the var_list will get:
day = {};

today={};

rain={};

mean={};

5 REPLIES 5
jthi
Super User

Re: take each item in list become an empty list using for loop

Don't do that. I would suggest creating those as keys to associative array and give them empty list as a value

Names Default To Here(1);

var_list = {"day", "today", "rain", "mean"};

aa = Associative Array(var_list, Repeat({{}}, N Items(var_list)));
-Jarmo
dadawasozo
Level IV

Re: take each item in list become an empty list using for loop

any negative reason why not to use for loop to do that?

also, I want to later do some calculation and append value to the associative array. Then turn the associative array into table. Below is the example how I do it. the aa's value is correct with {{}, {}, {"3", "2"}, {"A", "b"}}, but the table created only has value in :rain. I think it is because jmp default column data type is numeric+continous.

1) is there a better/faster way or forcing way to turn associative array into a table without the need of defining column data type?


keys = aa << get keys;
check= {"col_today", "col_rain"};
col_today = {"A", "b"};
col_rain = {"3","2"};

for (j=1, j<=nitems(check), j++,
for (i=1, i<=nitems(keys), i++,
	
	if(word(-1, check[j], "_") == keys[i],
		
		aa << Insert(keys[i],eval(parse(check[j])));
				
		
	);
);
);

values1 = aa << get values;
tb = new table();

for (i = 1, i <= nitems(keys), i++,
	
	tb << new column(char(keys[i]), , set values(values1[i]));
	
)
missing :today value because it is character
dadawasozo_0-1743010751753.png

 

jthi
Super User

Re: take each item in list become an empty list using for loop

Trying to deal with dynamically created variable names tends to be a mess and there are data structures to help with this type of scripting so you do not have to rely on dynamic variable names.

 

One option is to go through JSON and let JMP decide the data/modelling types

Names Default To Here(1);

aa = Associative Array();
aa["a"] = {"A", "B"};
aa["b"] = {"1", "2"};
aa["c"] = {1, 2};

dt = JSON To Data Table(As JSON Expr(aa), Guess(Stack(1)));

Or create all the columns and characters and then convert those which can be converted to numeric

-Jarmo
robot
Level VI

Re: take each item in list become an empty list using for loop

I agree with Jarmo's input, but I think this does what you ask:

 

Names Default To Here( 1 );

var_list = {"day", "today", "rain", "mean"};

var_list_list = {};
For Each( {var, i}, var_list, Insert Into( var_list_list, Parse( Eval Insert( "^var^ = {};" ) ) ) );
var_list_list;
jthi
Super User

Re: take each item in list become an empty list using for loop

I think you might have to add Eval List(var_list_list); to get the variables initialized once. 

 

I consider doing something like this such a bad idea that I don't even want to give an option for this, but still.. here is one

View more...
Names Default To Here(1);

var_list = {"day", "today", "rain", "mean"};

For Each({var, i}, var_list,
 Eval(EvalExpr(Assign(Expr(Parse(var)), {})));
);

show(day, today, rain, mean);
 
-Jarmo

Recommended Articles