cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
  • JMP 19 is here! See the new features at jmp.com/new.
  • Register to attend Discovery Summit 2025 Online: Early Users Edition, Sept. 24-25.
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 );
3 REPLIES 3
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

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

CoverMarmoset89_0-1758531601120.png

 

hogi
Level XII

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.

Recommended Articles