cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
dadawasozo
Level IV

how to change variable in multiple JSLs at once

Hi,

 

I use include() to run multiple JSLs in each JSL I have variable "todaydate". However, now I want to run all the JSLs using include, but change the todaydate across all JSLs to 03Jun2023. How I can do that ?

 

todaydate = Format date( Today(), "ddmonyyyy" );

I was thinking:
todaydate = "03Jun2023";  // this will replace in all 10 jsl

include(jsl1);

include(jsl2);

.

.

.

include(jsl10);

5 REPLIES 5
txnelson
Super User

Re: how to change variable in multiple JSLs at once

You will need to place the "todaydate" into a separate NameSpace.  This can either be a NameSpace you create, or you can use the global NameSpace defined by a ":".  Below are 2 scripts.  Run the first script, and then run the second, and you will see that the global NameSpace variable "a", referenced as "::a" is known across both JSL scripts.

Script 1

Names Default To Here( 1 );
a = 5;
::a = 7;

Script 2

Names Default To Here( 1 );
Try( Show( a ), Show( "a is not known" ) );
Show( ::a );
Jim
dadawasozo
Level IV

Re: how to change variable in multiple JSLs at once

Sorry, I m a bit slow here.

do you mean I will have to have my 10 jsl 's todaydate change to ::todaydate? does it mean all the todaydate occur at different lines that used in all 10jsl has to change to ::todaydate?

txnelson
Super User

Re: how to change variable in multiple JSLs at once

The short answer is yes.  I suggest you take the time to read the documentation on NameSpaces.

 

The only other way that could be used would be to always run all of your scripts in the global namespace.  The issue with that is that all variables will be available to all scripts, to if a variable named "X" is changed in one script, all scripts that have a variable named "X" will also be changed.

 

What I normally have done, is to define a namespace that I declare in all scripts.  And therefore, I can control all of the information that I want to be available to all of my scripts,   

 

names default to here(1);
if(isNamespace(nsCommon)==0,
	nsCommon = New Namespace("CommonSpace")
);
// Set today's date in the nsCommon namespace
nsCommon:todaydate = Format date( Today(), "ddmonyyyy" );

// set the local-to-this-script todaydate variable to the 
// value of the nsCommon value of todaydate.
todaydate = nsCommon:todaydate;
Jim
jthi
Super User

Re: how to change variable in multiple JSLs at once

There are many many different ways how you can handle this, but depends a lot on your scripts.

 

How is todaydate defined in your other scripts? What type of scripts are jsl1, jsl2...? Could you create functions/expressions and call them in main jsl file?

 

When you are using include it should work also in the other scripts unless you overwrite it those as inclusions will overwrite variables like Jim said, but you can "protect" them by using << New Context and << Names Default To Here in Include(). This of course will then prevent you from easily using the same variable in those scripts and using namespaces is fairly easy solution to fix this.

Names Default To Here(1);
Clear Log();

todaydate = Format Date(Today(), "ddmonyyyy");

jsl1_str = "\[Names Default To Here(1);
	Show(todaydate);
]\";

jsl2_str = "\[Names Default To Here(1);
	todaydate = 1;
	Show(todaydate);
]\";

Save Text File("$TEMP/661243_jsl1.jsl", jsl1_str);
Save Text File("$TEMP/661243_jsl2.jsl", jsl2_str);

Include("$TEMP/661243_jsl1.jsl");
Include("$TEMP/661243_jsl1.jsl", << New Context);
Include("$TEMP/661243_jsl1.jsl", << New Context, << Names Default To Here);
Include("$TEMP/661243_jsl2.jsl", << New Context);
show(todaydate); // not overwritten
Include("$TEMP/661243_jsl2.jsl", << New Context, << Names Default To Here);
show(todaydate); // not overwritten
Include("$TEMP/661243_jsl2.jsl");
show(todaydate); // overwritten

Try(Delete File("$TEMP/661243_jsl1.jsl"));
Try(Delete File("$TEMP/661243_jsl2.jsl"));

 

Scripting Guide has a lot of information regarding JMP Scripting, below are few links related to this topic

Scripting Guide > Programming Methods > Advanced Programming Concepts > Include a Script 

Scripting Guide > Programming Methods > Advanced Scoping and Namespaces 

Scripting Guide > Programming Methods > Advanced Programming Concepts > Advanced Functions 

Scripting Guide > JSL Building Blocks > Rules for Name Resolution 

Also remember to see more topics by pressing arrow in the help page

jthi_0-1690035268990.png

or using left side menu

jthi_1-1690035279854.png

 

-Jarmo
dadawasozo
Level IV

Re: how to change variable in multiple JSLs at once

Thank you so much for the information. These are new to me and definitely will help in my work.