<?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: Saving a new script to preserve variables for later in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Saving-a-new-script-to-preserve-variables-for-later/m-p/707442#M89179</link>
    <description>&lt;P&gt;I am not sure how universal this is, but it does work on your simple script&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;names default to here (1);

//namespace for the shared variables
If( !Namespace Exists( "TestNS" ), New Namespace( "TestNS" ) );

//the variables I want to save for later
TestNS:a = 5;
TestNS:b = "text";
TestNS:c = {1,2,3,4};
TestNS:d = {"a","b","c","d"};

//set up script to save as JSL
script = expr( 	
	TestNS:a = a_dv;
	TestNS:b = b_dv;
	TestNS:c = c_dv;
	TestNS:d = d_dv;
);

//substitute the dummy values for the current values... this isn't working
substitute Into (script, expr(a_dv), nameexpr (TestNS:a));
substitute Into (script, expr(b_dv), nameexpr (TestNS:b));
substitute Into (script, expr(c_dv), nameexpr (TestNS:c));
substitute Into (script, expr(d_dv), nameexpr (TestNS:d));

// Move the arguments in the script to a character string
theScript = "";
i=1;
While( char(arg(script,i)) !="Empty()",
	theScript = theScript || char(arg(script,i)) || ";";
	i++;
);

show(script);

//save script for later use
Save Text File( "$downloads\MyScript.jsl", theScript );&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 09 Dec 2023 03:16:42 GMT</pubDate>
    <dc:creator>txnelson</dc:creator>
    <dc:date>2023-12-09T03:16:42Z</dc:date>
    <item>
      <title>Saving a new script to preserve variables for later</title>
      <link>https://community.jmp.com/t5/Discussions/Saving-a-new-script-to-preserve-variables-for-later/m-p/707393#M89176</link>
      <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've built a script that performs data analysis but requires various user inputs to configure. There are cases where I want to perform a similar analysis later, but not need to re-enter all my settings. The main script already can read existing variables and auto-fill those fields, so if I have some way to save and import those default values, I'll be set. I want to be able to have multiple different configurations and not need to edit the script itself to make a new one.&amp;nbsp;&lt;/P&gt;&lt;P&gt;One idea was to save a table with a column of variable names and a column of their values, but that didn't play well with different variable types (numbers, strings, lists).&amp;nbsp;&lt;/P&gt;&lt;P&gt;A better-seeming idea is to save other .jsl files that contain the default settings for different use cases into a shared namespace, then run the relevant one before running my main script. I'd like to be able to create the configuration file from within my script, rather than manually making them.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I found this other post about saving new jsl files.&amp;nbsp;&lt;A href="https://community.jmp.com/t5/Discussions/how-do-you-create-a-new-script-file-from-jsl/td-p/34740" target="_blank"&gt;https://community.jmp.com/t5/Discussions/how-do-you-create-a-new-script-file-from-jsl/td-p/34740&lt;/A&gt;&lt;/P&gt;&lt;P&gt;It seems like I should be able to set up a JSL quote, then substitute the current values of variables into that quote, but NameExpr isn't working for that. Strings are substituted without quote marks, lists just appear as List and numbers are ignored. Is there a slight change that would make this work, or a better way to accomplish the same goal?&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;names default to here (1);

//namespace for the shared variables
If( !Namespace Exists( "TestNS" ), New Namespace( "TestNS" ) );

//the variables I want to save for later
TestNS:a = 5;
TestNS:b = "text";
TestNS:c = {1,2,3,4};
TestNS:d = {"a","b","c","d"};

//set up script to save as JSL
script = JSL Quote( 	
	TestNS:a = a_dv;
	TestNS:b = b_dv;
	TestNS:c = c_dv;
	TestNS:d = d_dv;
);

//substitute the dummy values for the current values... this isn't working
substitute Into (script, expr(a_dv), nameexpr (TestNS:a));
substitute Into (script, expr(b_dv), nameexpr (TestNS:b));
substitute Into (script, expr(c_dv), nameexpr (TestNS:c));
substitute Into (script, expr(d_dv), nameexpr (TestNS:d));

show(script);

//save script for later use
Save Text File( "$downloads\MyScript.jsl", script );&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 08 Dec 2023 23:25:50 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Saving-a-new-script-to-preserve-variables-for-later/m-p/707393#M89176</guid>
      <dc:creator>CitizenNo3</dc:creator>
      <dc:date>2023-12-08T23:25:50Z</dc:date>
    </item>
    <item>
      <title>Re: Saving a new script to preserve variables for later</title>
      <link>https://community.jmp.com/t5/Discussions/Saving-a-new-script-to-preserve-variables-for-later/m-p/707442#M89179</link>
      <description>&lt;P&gt;I am not sure how universal this is, but it does work on your simple script&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;names default to here (1);

//namespace for the shared variables
If( !Namespace Exists( "TestNS" ), New Namespace( "TestNS" ) );

//the variables I want to save for later
TestNS:a = 5;
TestNS:b = "text";
TestNS:c = {1,2,3,4};
TestNS:d = {"a","b","c","d"};

//set up script to save as JSL
script = expr( 	
	TestNS:a = a_dv;
	TestNS:b = b_dv;
	TestNS:c = c_dv;
	TestNS:d = d_dv;
);

//substitute the dummy values for the current values... this isn't working
substitute Into (script, expr(a_dv), nameexpr (TestNS:a));
substitute Into (script, expr(b_dv), nameexpr (TestNS:b));
substitute Into (script, expr(c_dv), nameexpr (TestNS:c));
substitute Into (script, expr(d_dv), nameexpr (TestNS:d));

// Move the arguments in the script to a character string
theScript = "";
i=1;
While( char(arg(script,i)) !="Empty()",
	theScript = theScript || char(arg(script,i)) || ";";
	i++;
);

show(script);

//save script for later use
Save Text File( "$downloads\MyScript.jsl", theScript );&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 09 Dec 2023 03:16:42 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Saving-a-new-script-to-preserve-variables-for-later/m-p/707442#M89179</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2023-12-09T03:16:42Z</dc:date>
    </item>
    <item>
      <title>Re: Saving a new script to preserve variables for later</title>
      <link>https://community.jmp.com/t5/Discussions/Saving-a-new-script-to-preserve-variables-for-later/m-p/707465#M89180</link>
      <description>&lt;P&gt;Have you maybe consider storing those values into a associative array? It is most likely easier to manage and it can be saved for example as a JSON if needed.&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);

//namespace for the shared variables
If(!Namespace Exists("TestNS"),
	New Namespace(
		"TestNS"
	)
);

//the variables I want to save for later
TestNS:settings = Associative Array();
TestNS:settings["a"] = 5;
TestNS:settings["b"] = "text";
TestNS:settings["c"] = [1 2 3 4];
TestNS:settings["d"] = {"a", "b", "c", "d"};
TestNS:settings["e"] = ["key" =&amp;gt; 1];


/*
TestNS:settings["a"] = cur_settings["a"];
TestNS:settings["b"] = cur_settings["b"];
TestNS:settings["c"] = cur_settings["c"];
TestNS:settings["d"] = cur_settings["d"];
TestNS:settings["e"] = cur_settings["e"];
*/

myfilepath = Save Text File("$DOWNLOADS/myscript.jsl", As JSON Expr(TestNS:settings));

conf = Load Text File(myfilepath);
loaded_settings = Parse JSON(conf);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could then create few functions such as save_settings, load_settings, ... if needed.&lt;/P&gt;</description>
      <pubDate>Sat, 09 Dec 2023 06:21:28 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Saving-a-new-script-to-preserve-variables-for-later/m-p/707465#M89180</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2023-12-09T06:21:28Z</dc:date>
    </item>
    <item>
      <title>Re: Saving a new script to preserve variables for later</title>
      <link>https://community.jmp.com/t5/Discussions/Saving-a-new-script-to-preserve-variables-for-later/m-p/710464#M89442</link>
      <description>&lt;P&gt;Thanks to both Jarmo and Jim, both those solutions work. I initially used Jim's solution, which was able to plug directly into what I initially had. After some revision, I adapted Jarmo's idea which indeed was tidier to manage. Here is what I ended up with. With this setup I didn't need the namespace, but it would still work with namespaces.&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);

//initialize variables
var1 = {"a", "b", "c"};
var2 = 100;
var3 = [1, 2, 3, 4];

//initialize variables used for functions
VarsToExport = {"var1", "var2", "var3"};
SettingsAA = Associative Array();
loaded_settingsAA = .;
SelfPath = Get Default Directory();

//expr to get each variable in the VarsToExport list and input the value into an AA, export as JSON
ExportVarsExpr = expr(
	for each({varname}, VarsToExport,
		//show(varname);
		SettingsAA[varname] = eval(as name(varname));
		//show(SettingsAA);
	);
	
	VarsSavePath = Pick File(
		"Save a file containing current script settings",
		SelfPath,
		{"JMP Script Files|jsl", "All Files|*"},
		1,
		1,
		"Layer_Settings.jsl"
	);

	Save Text File(VarsSavePath, As JSON Expr(settingsAA));
);

//expr open a jsl file, parse JSON into an AA and extract saved values
ImportVarsExpr = expr(
	VarsImportPath = Pick File(
		"Select jsl file that contains the saved parameters",
		SelfPath,
		{"JMP Script Files|jsl", "All Files|*"},
		1,
		0,
		""
	);

	loaded_settingsAA = Parse JSON(Load Text File(VarsImportPath));

	//for each element of AA, get the name of the element and set a variable with that name to the AA value
	for each({varname}, loaded_settingsAA,
		eval(parse(Evalinsert("\[  "^varname^"n = loaded_settingsAA &amp;lt;&amp;lt; get value (varname) ]\")));
		//show(varname);
		//show(loaded_settingsAA &amp;lt;&amp;lt; get value (varname));
	);
);

//show original variables
Write("\!N", "Original:");
show(var1, var2, var2);

//export variables
eval(ExportVarsExpr);

//clear the variables
var1 = .;
var2 = .;
var3 = .;
Write("\!N", "\!N", "Variables cleared:");
show(var1, var2, var2);

//import variables
eval(ImportVarsExpr);

//show that the variables have been imported
Write("\!N", "\!N", "Post loading:");
show(var1, var2, var2);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 19 Dec 2023 18:44:51 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Saving-a-new-script-to-preserve-variables-for-later/m-p/710464#M89442</guid>
      <dc:creator>CitizenNo3</dc:creator>
      <dc:date>2023-12-19T18:44:51Z</dc:date>
    </item>
  </channel>
</rss>

