cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Discussions

Solve problems, and share tips and tricks with other JMP users.
Choose Language Hide Translation Bar
BabyDoragon
Level II

Regarding the issue of generating dynamic variable names using nested loops

Regarding the dynamic variable issue, here is the JSL below. I want to generate variable names combining fruit and month, for example, Apple_Jun. However, the following JSL does not run. How should I fix it?

 

FruitList = {"Apple", "Banana", "Cherry", "Durian", "Entawak"};
MonthList = {"_Jan", "_Feb", "_March", "_April", "_May"};
For( count = 1, count <= N Items( FruitList ), count++,
	For( countM = 1, countM <= N Items( MonthList ), countM++, 

		ex = Expr(
			Parse( Fruit || Month ) = 1;
			
		);
		Substitute Into( ex,
			Expr( Fruit ), FruitList[Count],
			Expr( Month ),
				MonthList[CountM]
			
		);
	);
	Eval( ex );
);

Show( Apple_Jan );
4 REPLIES 4
txnelson
Super User

Re: Regarding the issue of generating dynamic variable names using nested loops

You have made the JSL far more complex than required

Names Default To Here( 1 );
FruitList = {"Apple", "Banana", "Cherry", "Durian", "Entawak"};
MonthList = {"_Jan", "_Feb", "_March", "_April", "_May"};
ColNamesList = {};
For Each( {Month}, MonthList,
	For Each( {Fruit}, FruitList,
		Insert Into( ColNamesList, Fruit || Month )
	)
);
Show( ColNamesList );
Jim
lwencx
Level II

Re: Regarding the issue of generating dynamic variable names using nested loops

CoverMarmoset89_0-1758531601120.png

 

hogi
Level XIII

Re: Regarding the issue of generating dynamic variable names using nested loops

One of the few places where Parse() is needed - and there seems to be no easier approach via Expression handling:

generation of variable names.

 

On the other hand:

In 99% of such cases, one can zoom out one step and search for an easier approach for the main target.
One which doesn't rely on custom made variable names.

e.g. use a 2D array, nested lists or associative array of associative arrays.

fruit_month
and indexing like

fruit_month[i,j], (fruit_month[i])[j] or (fruit_month[month])[fruit]

 

instead of a "2D-Array" of variable names.

jthi
Super User

Re: Regarding the issue of generating dynamic variable names using nested loops

There are very very limited cases where you might generate dynamic variable names and usually even with those, you shouldn't. Instead, consider using proper data structure (table, associative array, lists, ...). 

 

Example using associative array

Names Default To Here(1);

FruitList = {"Apple", "Banana", "Cherry", "Durian", "Entawak"};
MonthList = {"_Jan", "_Feb", "_March", "_April", "_May"};

aa = Associative Array(FruitList, Repeat({Associative Array(MonthList, As List(Repeat(1, N Items(MonthList))))}, N Items(FruitList)));

aa = Associative Array();
For Each({fruit}, fruitlist,
	For Each({month}, monthlist,
		If(!Contains(aa, fruit),
			aa[fruit] = Associative Array();
		);
		aa[fruit][month] = 1;
	);	
);
Show(aa["Apple"]["_Jan"]);

Difficult to read version

Names Default To Here(1);

FruitList = {"Apple", "Banana", "Cherry", "Durian", "Entawak"};
MonthList = {"_Jan", "_Feb", "_March", "_April", "_May"};

aa = Associative Array(FruitList, Repeat({Associative Array(MonthList, As List(Repeat(1, N Items(MonthList))))}, N Items(FruitList)));

Show(aa["Apple"]["_Jan"]);
-Jarmo

Recommended Articles