cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
  • JMP will suspend normal business operations for our Winter Holiday beginning on Wednesday, Dec. 24, 2025, at 5:00 p.m. ET (2:00 p.m. ET for JMP Accounts Receivable).
    Regular business hours will resume at 9:00 a.m. EST on Friday, Jan. 2, 2026.
  • We’re retiring the File Exchange at the end of this year. The JMP Marketplace is now your destination for add-ins and extensions.

Discussions

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

Global Variables overwritten in a loop

Hello,

I create windows in a loop following columns. For example, in the JSL example below, I create a window with the distribution and a button that temporarily hides the window for each column (Age, Weight). The problem is that as my loop progresses, my object variables are overwritten by the last window.

For example, in the age window generated by the example, clicking on the button is supposed to temporarily hide the Age window, but in reality it hides Weight because it has been overwritten.

Is it possible to do a names default to here or something like that so that each window has its own independent space without having to change the name of the variables for each window every time? Because I have a lot of them...

 

 

Names Default To Here( 1 );
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
lstCols = {"age", "weight"};

For Each( {value, index}, lstCols, 

	colref = Column( value );
	
	window = New Window( "Window - " || value, 
		
		V List Box(
			distribution = dt << Distribution( Column( colref ) ), 
			
			Button Box( "Hide",
				window << show window( 0 );
				Wait( 2 );
				window << show window( 1 );
			)
		);

	);
);

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
jthi
Super User

Re: Global Variables overwritten in a loop

You can use window scope

Names Default To Here(1);

dt = Open("$SAMPLE_DATA/Big Class.jmp");
lstCols = {"age", "weight"};

nws = {};

For Each({value, index}, lstCols, 
	colref = Column(value);
	
	nw = New Window("Window - " || value, 
		V List Box(
			window:dist = dt << Distribution(Column(colref)), 	
			Button Box("Hide",
				window:dist << show window(0);
				Wait(2);
				window:dist << show window(1);
			)
		);
	);
	Insert Into(nws, nw);
);

For Each({nw}, nws,
	Show(nw:dist << Get Window Title)
);

You could also evaluate the reference to the button script/function or use slightly more complicated function within the button to take scoping into account.

-Jarmo

View solution in original post

6 REPLIES 6
jthi
Super User

Re: Global Variables overwritten in a loop

You can use window scope

Names Default To Here(1);

dt = Open("$SAMPLE_DATA/Big Class.jmp");
lstCols = {"age", "weight"};

nws = {};

For Each({value, index}, lstCols, 
	colref = Column(value);
	
	nw = New Window("Window - " || value, 
		V List Box(
			window:dist = dt << Distribution(Column(colref)), 	
			Button Box("Hide",
				window:dist << show window(0);
				Wait(2);
				window:dist << show window(1);
			)
		);
	);
	Insert Into(nws, nw);
);

For Each({nw}, nws,
	Show(nw:dist << Get Window Title)
);

You could also evaluate the reference to the button script/function or use slightly more complicated function within the button to take scoping into account.

-Jarmo
Mittman
Staff

Re: Global Variables overwritten in a loop

Re: Global Variables overwritten in a loop

Hello thank you ! It solved my issue

jthi
Super User

Re: Global Variables overwritten in a loop

You can also use << Set Function and use the buttons reference as the "starting" point

Names Default To Here(1);

dt = Open("$SAMPLE_DATA/Big Class.jmp");
lstCols = {"age", "weight"};

For Each({colname}, lstCols,
	nw = New Window("Window - " || colname, 
		V List Box(
			dt << Distribution(Column(Eval(colname))), 
			Button Box("Hide", << Set Function(Function({this},
				dist_ob = this << prev sib;
				show(dist_ob[OutlineBox(2)] << get title);
			)));
		);
	);
);

Write();

and in this case for hiding the window you could just use this << Show Window(0);

Context Box can also be sometimes used:

Names Default To Here(1);

dt = Open("$SAMPLE_DATA/Big Class.jmp");
lstCols = {"age", "weight"};

For Each({colname}, lstCols,
	nw = New Window("Window - " || colname,
		V List Box(
			Context Box(
				box:dist = dt << Distribution(Column(Eval(colname))), 
				Button Box("Hide", 
					show(Report(box:dist)[OutlineBox(2)] << get title)
				);
			)
		);
	);
);

Write();

I use all these methods (and expression evaluation) depending on the situation.

 

-Jarmo
Mittman
Staff

Re: Global Variables overwritten in a loop

Below is a solution that isn' as elegant as @jthi 's answer, but more intuitive from my perspective.

Names Default To Here( 1 );
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
lstCols = {"age", "weight"};

windows = Associative Array();

For Each( {value, index}, lstCols, 

	colref = Column( value );
	
	Eval(EvalExpr(
		windows[value] = New Window( "Window - " || value, 
			
			V List Box(
				distribution = dt << Distribution( Column( colref ) ), 
				
				Button Box( "Hide",
					windows[Expr(value)] << show window( 0 );
					Wait( 2 );
					windows[Expr(value)] << show window( 1 );
				)
			);

		);
	));
);

The associative array "windows" provides a container that maintains references to the different windows and "Eval(EvalExpr(...))" is used to make sure that a particular button's script isn't evaluating the current "value" but the one in the scope when it was created.

 

Re: Global Variables overwritten in a loop

Hello, thank you for your answer it is another way to do it!

Recommended Articles