<?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: How does scope of variables work in JSL in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/How-does-scope-of-variables-work-in-JSL/m-p/903326#M106234</link>
    <description>&lt;P&gt;An important detail:&lt;BR /&gt;The script window is just part of the &lt;FONT face="courier new,courier"&gt;Here&lt;/FONT&gt; namespace.&lt;/P&gt;
&lt;P&gt;Windows and reports created from the '&lt;FONT face="courier new,courier"&gt;Here&lt;/FONT&gt;' namespace have access to it as well.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Simply run the code, close the script window, and observe the behavior of &lt;FONT face="courier new,courier"&gt;x&lt;/FONT&gt; in the &lt;FONT face="courier new,courier"&gt;Here&lt;/FONT&gt; namespace.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default to Here(1);
x=5;
Caption("x is" || Char(here:x));
x=6;
new window("Here", Button box ("X= ? ", Caption("x = " || Char(here:x));x++));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 24 Sep 2025 11:12:20 GMT</pubDate>
    <dc:creator>hogi</dc:creator>
    <dc:date>2025-09-24T11:12:20Z</dc:date>
    <item>
      <title>How does scope of variables work in JSL</title>
      <link>https://community.jmp.com/t5/Discussions/How-does-scope-of-variables-work-in-JSL/m-p/82905#M37355</link>
      <description>&lt;P&gt;I'm sure this is posted somewhere already, but I wasn't able to find it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm confused how the scope of variables is handled in JSL.&amp;nbsp; Could someone help to clarify how it works.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I almost always use i as the indexing variable for loops.&amp;nbsp; For example, in my script I will have a for loop with i as the index that calls another function.&amp;nbsp; In this function, there is another for loop with i as the index.&amp;nbsp; This has never been a problem in C/Java/etc.&amp;nbsp; It seems that sometimes it works in jsl and other times it doesn't.&amp;nbsp; I don't have an example because I havn't been able to figure out exactly where the problem occurs.&amp;nbsp; I think it is a basic understanding of the scope of variables in JSL though.&lt;/P&gt;</description>
      <pubDate>Thu, 08 Nov 2018 22:10:28 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-does-scope-of-variables-work-in-JSL/m-p/82905#M37355</guid>
      <dc:creator>noblea30</dc:creator>
      <dc:date>2018-11-08T22:10:28Z</dc:date>
    </item>
    <item>
      <title>Re: How does scope of variables work in JSL</title>
      <link>https://community.jmp.com/t5/Discussions/How-does-scope-of-variables-work-in-JSL/m-p/82916#M37358</link>
      <description>&lt;P&gt;In JSL, a variable, lets use your example of a variable called "i" is described as&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp; namespace:i&lt;/P&gt;
&lt;P&gt;If nothing is specified to set a namespace, the formal specification is:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp; ::i&lt;/P&gt;
&lt;P&gt;Since the default namespace in JSL is a namespace named ":"......pretty wierd, but there are historical reasons for this.&amp;nbsp; So if you enter in&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp; i = 7;&lt;/P&gt;
&lt;P&gt;What JMP is seeing is:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp; ::i = 7;&lt;/P&gt;
&lt;P&gt;So, now if in any part of a JSL script, the variable i is changed, and then referenced later on, it will have the new value&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;showit = Function( {I},
     Show(I);
);
I = 2;
showit(I);

I=3;
showit(I);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;it returns&lt;/P&gt;
&lt;PRE&gt;I = 2;
I = 3;&lt;/PRE&gt;
&lt;P&gt;Now, a function can isolate a variable, by making the variable local to the function&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;showit = Function( {I},{I},
     I=55;
     Show(I);
);
I = 2;
showit(I);

show(i);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The result of this code is:&lt;/P&gt;
&lt;PRE&gt;I = 55;
i = 2;&lt;/PRE&gt;
&lt;P&gt;notice, that the value of i for the global use of it has not changed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now for some final confusion:&lt;/P&gt;
&lt;P&gt;When I said that the value of i can be changed, and all subsequent uses of i will reflect that change, that means it will be changed even in different scripts that are being run.&amp;nbsp; So if a new script is run that has the variable i in it, it will have the value of the i that was set in the previous JSL script.&amp;nbsp; Thus, the statement "Names Default to Here( 1 );" was implemented.&amp;nbsp; This statement does 2 things.&amp;nbsp; First, it changes the namespace from ":" to "Here".&amp;nbsp; Seconly, it isolates all of the variables in the "Here" namespace to only the variables in the script that is being run.&amp;nbsp; So now, if you have the following script&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default to Here( 1 );
i = 7;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;this instance of the variable i will only be changed by the usage of i within the script.....no other script will have access to the changing of that variable i.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;P.S. &amp;nbsp; The formal description of the variable i is:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp; here:i&lt;/P&gt;
&lt;P&gt;and any variable in the script that is created in the script, after the "Names Default to Here( 1 );" statement will automatically be placed into the namespace call Here. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Nov 2018 22:38:26 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-does-scope-of-variables-work-in-JSL/m-p/82916#M37358</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2018-11-08T22:38:26Z</dc:date>
    </item>
    <item>
      <title>Re: How does scope of variables work in JSL</title>
      <link>https://community.jmp.com/t5/Discussions/How-does-scope-of-variables-work-in-JSL/m-p/903169#M106215</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default to Here( 1 );
i = 7;
show(here:i)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I cannot use the "here:" to explicitly address variables in there here namespace?&lt;BR /&gt;&lt;BR /&gt;Why are columns addresses via e.g. "&lt;FONT face="courier new,courier"&gt;:sex&lt;/FONT&gt;".&lt;BR /&gt;Empty namespace means: "&lt;FONT face="courier new,courier"&gt;current data table()&lt;/FONT&gt;" ?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 23 Sep 2025 22:03:57 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-does-scope-of-variables-work-in-JSL/m-p/903169#M106215</guid>
      <dc:creator>hogi</dc:creator>
      <dc:date>2025-09-23T22:03:57Z</dc:date>
    </item>
    <item>
      <title>Re: How does scope of variables work in JSL</title>
      <link>https://community.jmp.com/t5/Discussions/How-does-scope-of-variables-work-in-JSL/m-p/903175#M106216</link>
      <description>&lt;P&gt;Your code works just fine for me.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;//:*/
Names Default to Here( 1 );
i = 7;
show(here:i)
/*:

here:i = 7;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Original JMP notation was a single colon was a data table column reference, and a double colon was a memory variable reference.&amp;nbsp; If the colon(s) were left off, typically JMP assumed it was a global memory variable.&lt;/P&gt;
&lt;P&gt;When Namespaces came around, the notation changed to Namespace : memory variable&amp;nbsp; &amp;nbsp;and Data Table() : data table column.&lt;/P&gt;
&lt;P&gt;Names Default to Here(1); changed the default namespace from the Global Namespace, to a Namespace named Here. Variable referenced without a colon would now use the Namespace "Here"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 24 Sep 2025 00:52:58 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-does-scope-of-variables-work-in-JSL/m-p/903175#M106216</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2025-09-24T00:52:58Z</dc:date>
    </item>
    <item>
      <title>Re: How does scope of variables work in JSL</title>
      <link>https://community.jmp.com/t5/Discussions/How-does-scope-of-variables-work-in-JSL/m-p/903210#M106217</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/2687"&gt;@txnelson&lt;/a&gt;&amp;nbsp;.&lt;BR /&gt;many thanks for the background info about the history of scoping and namespaces.&lt;BR /&gt;Knowing where it came from helps a lot to understand why it's like this now .&lt;BR /&gt;&lt;BR /&gt;Thanks for double checking my code.&lt;BR /&gt;Hm, let's check the symbols in the memory ...&lt;BR /&gt;Ouch! I must have forgotten to run the&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default to Here( 1 );&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;in my code.&lt;/P&gt;</description>
      <pubDate>Wed, 24 Sep 2025 11:01:56 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-does-scope-of-variables-work-in-JSL/m-p/903210#M106217</guid>
      <dc:creator>hogi</dc:creator>
      <dc:date>2025-09-24T11:01:56Z</dc:date>
    </item>
    <item>
      <title>Re: How does scope of variables work in JSL</title>
      <link>https://community.jmp.com/t5/Discussions/How-does-scope-of-variables-work-in-JSL/m-p/903293#M106228</link>
      <description>&lt;P&gt;There is some pages in the jmp help that explains how scope works in jmp, and how names are resolved:&lt;BR /&gt;&lt;A href="https://www.jmp.com/support/help/en/18.2/index.shtml#page/jmp/local-namespaces.shtml#" target="_blank"&gt;Local Namespaces&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://www.jmp.com/support/help/en/18.2/index.shtml#page/jmp/advanced-scoping-and-namespaces.shtml#ww339298" target="_blank"&gt;Advanced Scoping and Namespaces&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://www.jmp.com/support/help/en/18.2/index.shtml#page/jmp/reference-namespaces-and-scopes.shtml#" target="_blank"&gt;Reference Namespaces and Scopes&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;By default most variables will be put in the global scope, shared across the jmp session.&lt;/P&gt;
&lt;P&gt;Running the script&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;a = 1;
show(a); &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;will show a = 1&lt;/P&gt;
&lt;P&gt;running&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;show(a);
a = 3;
show(a);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;will show a = 1, then a = 3 as the variable is shared.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Scripts can be (somewhat) isolated by putting Names Default to Here(1) in the script.&lt;/P&gt;
&lt;P&gt;This creates a scope "here" that is only accessible by the script, and exist for the lifetime of the script&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);
show(isempty(a));
a = 1;
show(isempty(a));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If the above script will show 1, 0 the first time its run and 0, 0 the second time its run, closing the script window resets the here scope.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There is also local (and local here) blocks, where we can specify variables that a only accessible to that part of the code.&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);
a = 1;
b = 2;
show(a); // 1
local( {a},
	a = 3;
	show(a); // 3
	show(b); // 2
);
show(a); // 1&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Finally there are namespaces, which creates a "place" where we can put variables.&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);
ns = New Namespace("test");
a = 1;
ns:a = 3;
show(a, ns:a); // 1, 3&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that namespaces are globally accessible, so if you have two scripts that both a namespace with the same name, they will access the same variables. This can be mitigated by using anonymous namespaces.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What can trick people, and sounds like the problem you're having is that variables in functions default to Global/Here scope.&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);
f1 = Function({},
	a = 1;
);
a = 2;
show(a); // 2
f1();
show(a) // 1
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;There is the option to either specify which variables should be local to the function, as txnelson explained. there is also the {Default Local Option}&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default to Here(1);
f1 = Function({},
	i = i+1;
);
f2 = Function({},{Default Local},
	i = i+1;
);

//shows 2,4
For(i = 1, i &amp;lt;= 5, i++,
	f1();
	show(i)
);

//shows 1,2,3,4,5
For(i = 1, i &amp;lt;= 5, i++,
	f2();
	show(i)
)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It sounds like this is the problem you mentioned.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Other peculiarities:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;There some peculiarities with functions compared to most other languages. if a variable isn't defined in the local scope JMP will look at the surrounding scope. For functions this is the entire call stack.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default to Here(1);
show_fun = Function({},{Default Local},
	show(a,b);
);

f1 = Function({},{Default Local},
	a = 3;
	show_fun();
);

f2 = Function({},{Default Local},
	b = 4;
	f1();
);

a = 1;
b = 2;

show_fun(); // 1,2
f1(); // 3,2
f2(); // 3,4&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 24 Sep 2025 09:25:29 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-does-scope-of-variables-work-in-JSL/m-p/903293#M106228</guid>
      <dc:creator>KasperSchlosser</dc:creator>
      <dc:date>2025-09-24T09:25:29Z</dc:date>
    </item>
    <item>
      <title>Re: How does scope of variables work in JSL</title>
      <link>https://community.jmp.com/t5/Discussions/How-does-scope-of-variables-work-in-JSL/m-p/903294#M106229</link>
      <description>&lt;P&gt;hi&amp;nbsp;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/46858"&gt;@KasperSchlosser&lt;/a&gt;&amp;nbsp;, very nice overview of Scoping : )&lt;/P&gt;</description>
      <pubDate>Wed, 24 Sep 2025 09:31:32 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-does-scope-of-variables-work-in-JSL/m-p/903294#M106229</guid>
      <dc:creator>hogi</dc:creator>
      <dc:date>2025-09-24T09:31:32Z</dc:date>
    </item>
    <item>
      <title>Re: How does scope of variables work in JSL</title>
      <link>https://community.jmp.com/t5/Discussions/How-does-scope-of-variables-work-in-JSL/m-p/903326#M106234</link>
      <description>&lt;P&gt;An important detail:&lt;BR /&gt;The script window is just part of the &lt;FONT face="courier new,courier"&gt;Here&lt;/FONT&gt; namespace.&lt;/P&gt;
&lt;P&gt;Windows and reports created from the '&lt;FONT face="courier new,courier"&gt;Here&lt;/FONT&gt;' namespace have access to it as well.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Simply run the code, close the script window, and observe the behavior of &lt;FONT face="courier new,courier"&gt;x&lt;/FONT&gt; in the &lt;FONT face="courier new,courier"&gt;Here&lt;/FONT&gt; namespace.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default to Here(1);
x=5;
Caption("x is" || Char(here:x));
x=6;
new window("Here", Button box ("X= ? ", Caption("x = " || Char(here:x));x++));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 24 Sep 2025 11:12:20 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-does-scope-of-variables-work-in-JSL/m-p/903326#M106234</guid>
      <dc:creator>hogi</dc:creator>
      <dc:date>2025-09-24T11:12:20Z</dc:date>
    </item>
  </channel>
</rss>

