<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Global Variables overwritten in a loop in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Global-Variables-overwritten-in-a-loop/m-p/920999#M107979</link>
    <description>&lt;P&gt;You can use window scope&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;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 &amp;lt;&amp;lt; Distribution(Column(colref)), 	
			Button Box("Hide",
				window:dist &amp;lt;&amp;lt; show window(0);
				Wait(2);
				window:dist &amp;lt;&amp;lt; show window(1);
			)
		);
	);
	Insert Into(nws, nw);
);

For Each({nw}, nws,
	Show(nw:dist &amp;lt;&amp;lt; Get Window Title)
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;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.&lt;/P&gt;</description>
    <pubDate>Mon, 22 Dec 2025 10:01:55 GMT</pubDate>
    <dc:creator>jthi</dc:creator>
    <dc:date>2025-12-22T10:01:55Z</dc:date>
    <item>
      <title>Global Variables overwritten in a loop</title>
      <link>https://community.jmp.com/t5/Discussions/Global-Variables-overwritten-in-a-loop/m-p/920987#M107977</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;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, m&lt;STRONG&gt;y object variables are overwritten by the last window&lt;/STRONG&gt;.&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;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...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;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 &amp;lt;&amp;lt; Distribution( Column( colref ) ), 
			
			Button Box( "Hide",
				window &amp;lt;&amp;lt; show window( 0 );
				Wait( 2 );
				window &amp;lt;&amp;lt; show window( 1 );
			)
		);

	);
);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 22 Dec 2025 08:38:34 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Global-Variables-overwritten-in-a-loop/m-p/920987#M107977</guid>
      <dc:creator>SophieCuvillier</dc:creator>
      <dc:date>2025-12-22T08:38:34Z</dc:date>
    </item>
    <item>
      <title>Re: Global Variables overwritten in a loop</title>
      <link>https://community.jmp.com/t5/Discussions/Global-Variables-overwritten-in-a-loop/m-p/920999#M107979</link>
      <description>&lt;P&gt;You can use window scope&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;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 &amp;lt;&amp;lt; Distribution(Column(colref)), 	
			Button Box("Hide",
				window:dist &amp;lt;&amp;lt; show window(0);
				Wait(2);
				window:dist &amp;lt;&amp;lt; show window(1);
			)
		);
	);
	Insert Into(nws, nw);
);

For Each({nw}, nws,
	Show(nw:dist &amp;lt;&amp;lt; Get Window Title)
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Mon, 22 Dec 2025 10:01:55 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Global-Variables-overwritten-in-a-loop/m-p/920999#M107979</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2025-12-22T10:01:55Z</dc:date>
    </item>
    <item>
      <title>Re: Global Variables overwritten in a loop</title>
      <link>https://community.jmp.com/t5/Discussions/Global-Variables-overwritten-in-a-loop/m-p/921086#M107984</link>
      <description>&lt;P&gt;Below is a solution that isn' as elegant as &lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/14366"&gt;@jthi&lt;/a&gt;&amp;nbsp;'s answer, but more intuitive from my perspective.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;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 &amp;lt;&amp;lt; Distribution( Column( colref ) ), 
				
				Button Box( "Hide",
					windows[Expr(value)] &amp;lt;&amp;lt; show window( 0 );
					Wait( 2 );
					windows[Expr(value)] &amp;lt;&amp;lt; show window( 1 );
				)
			);

		);
	));
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 23 Dec 2025 12:28:55 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Global-Variables-overwritten-in-a-loop/m-p/921086#M107984</guid>
      <dc:creator>Mittman</dc:creator>
      <dc:date>2025-12-23T12:28:55Z</dc:date>
    </item>
    <item>
      <title>Re: Global Variables overwritten in a loop</title>
      <link>https://community.jmp.com/t5/Discussions/Global-Variables-overwritten-in-a-loop/m-p/921087#M107985</link>
      <description>&lt;P&gt;+1. Window scope is described here:&amp;nbsp;&lt;A href="https://www.jmp.com/support/help/en/19.1/?os=mac&amp;amp;source=application#page/jmp/scoped-names.shtml" target="_self"&gt;https://www.jmp.com/support/help/en/19.1/?os=mac&amp;amp;source=application#page/jmp/scoped-names.shtml&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 23 Dec 2025 12:29:35 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Global-Variables-overwritten-in-a-loop/m-p/921087#M107985</guid>
      <dc:creator>Mittman</dc:creator>
      <dc:date>2025-12-23T12:29:35Z</dc:date>
    </item>
    <item>
      <title>Re: Global Variables overwritten in a loop</title>
      <link>https://community.jmp.com/t5/Discussions/Global-Variables-overwritten-in-a-loop/m-p/921216#M108000</link>
      <description>&lt;P&gt;Hello thank you ! It solved my issue&lt;/P&gt;</description>
      <pubDate>Wed, 24 Dec 2025 14:20:12 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Global-Variables-overwritten-in-a-loop/m-p/921216#M108000</guid>
      <dc:creator>SophieCuvillier</dc:creator>
      <dc:date>2025-12-24T14:20:12Z</dc:date>
    </item>
    <item>
      <title>Re: Global Variables overwritten in a loop</title>
      <link>https://community.jmp.com/t5/Discussions/Global-Variables-overwritten-in-a-loop/m-p/921217#M108001</link>
      <description>&lt;P&gt;Hello, thank you for your answer it is another way to do it!&lt;/P&gt;</description>
      <pubDate>Wed, 24 Dec 2025 14:20:59 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Global-Variables-overwritten-in-a-loop/m-p/921217#M108001</guid>
      <dc:creator>SophieCuvillier</dc:creator>
      <dc:date>2025-12-24T14:20:59Z</dc:date>
    </item>
    <item>
      <title>Re: Global Variables overwritten in a loop</title>
      <link>https://community.jmp.com/t5/Discussions/Global-Variables-overwritten-in-a-loop/m-p/921221#M108003</link>
      <description>&lt;P&gt;You can also use &amp;lt;&amp;lt; Set Function and use the buttons reference as the "starting" point&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;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 &amp;lt;&amp;lt; Distribution(Column(Eval(colname))), 
			Button Box("Hide", &amp;lt;&amp;lt; Set Function(Function({this},
				dist_ob = this &amp;lt;&amp;lt; prev sib;
				show(dist_ob[OutlineBox(2)] &amp;lt;&amp;lt; get title);
			)));
		);
	);
);

Write();&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and in this case for hiding the window you could just use &lt;EM&gt;this &amp;lt;&amp;lt; Show Window(0);&lt;CODE class=" language-jsl"&gt;&lt;/CODE&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;Context Box can also be sometimes used:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;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 &amp;lt;&amp;lt; Distribution(Column(Eval(colname))), 
				Button Box("Hide", 
					show(Report(box:dist)[OutlineBox(2)] &amp;lt;&amp;lt; get title)
				);
			)
		);
	);
);

Write();&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I use all these methods (and expression evaluation) depending on the situation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 24 Dec 2025 17:33:42 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Global-Variables-overwritten-in-a-loop/m-p/921221#M108003</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2025-12-24T17:33:42Z</dc:date>
    </item>
  </channel>
</rss>

