cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
Choose Language Hide Translation Bar
noblea30
Level I

How does scope of variables work in JSL

I'm sure this is posted somewhere already, but I wasn't able to find it.

 

I'm confused how the scope of variables is handled in JSL.  Could someone help to clarify how it works.

 

I almost always use i as the indexing variable for loops.  For example, in my script I will have a for loop with i as the index that calls another function.  In this function, there is another for loop with i as the index.  This has never been a problem in C/Java/etc.  It seems that sometimes it works in jsl and other times it doesn't.  I don't have an example because I havn't been able to figure out exactly where the problem occurs.  I think it is a basic understanding of the scope of variables in JSL though.

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: How does scope of variables work in JSL

In JSL, a variable, lets use your example of a variable called "i" is described as

     namespace:i

If nothing is specified to set a namespace, the formal specification is:

     ::i

Since the default namespace in JSL is a namespace named ":"......pretty wierd, but there are historical reasons for this.  So if you enter in

     i = 7;

What JMP is seeing is:

     ::i = 7;

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

showit = Function( {I},
     Show(I);
);
I = 2;
showit(I);

I=3;
showit(I);

it returns

I = 2;
I = 3;

Now, a function can isolate a variable, by making the variable local to the function

showit = Function( {I},{I},
     I=55;
     Show(I);
);
I = 2;
showit(I);

show(i);

The result of this code is:

I = 55;
i = 2;

notice, that the value of i for the global use of it has not changed.

 

Now for some final confusion:

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.  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.  Thus, the statement "Names Default to Here( 1 );" was implemented.  This statement does 2 things.  First, it changes the namespace from ":" to "Here".  Seconly, it isolates all of the variables in the "Here" namespace to only the variables in the script that is being run.  So now, if you have the following script

Names Default to Here( 1 );
i = 7;

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.

 

P.S.   The formal description of the variable i is:

     here:i

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.  

 

Jim

View solution in original post

1 REPLY 1
txnelson
Super User

Re: How does scope of variables work in JSL

In JSL, a variable, lets use your example of a variable called "i" is described as

     namespace:i

If nothing is specified to set a namespace, the formal specification is:

     ::i

Since the default namespace in JSL is a namespace named ":"......pretty wierd, but there are historical reasons for this.  So if you enter in

     i = 7;

What JMP is seeing is:

     ::i = 7;

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

showit = Function( {I},
     Show(I);
);
I = 2;
showit(I);

I=3;
showit(I);

it returns

I = 2;
I = 3;

Now, a function can isolate a variable, by making the variable local to the function

showit = Function( {I},{I},
     I=55;
     Show(I);
);
I = 2;
showit(I);

show(i);

The result of this code is:

I = 55;
i = 2;

notice, that the value of i for the global use of it has not changed.

 

Now for some final confusion:

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.  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.  Thus, the statement "Names Default to Here( 1 );" was implemented.  This statement does 2 things.  First, it changes the namespace from ":" to "Here".  Seconly, it isolates all of the variables in the "Here" namespace to only the variables in the script that is being run.  So now, if you have the following script

Names Default to Here( 1 );
i = 7;

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.

 

P.S.   The formal description of the variable i is:

     here:i

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.  

 

Jim