<?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: Anyone have a good JSL .ini file reader? in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Anyone-have-a-good-JSL-ini-file-reader/m-p/441801#M69010</link>
    <description>&lt;P&gt;I routinely read and write associative arrays to an Oracle database.&amp;nbsp; This is a similar approach for an .ini file:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;/*
Function Name: char_to_aa

Description:    Convert a character string to an associative array
*/
char_to_aa = Function({one_char_aa},

	if (contains(one_char_aa, "Associative Array"),
		one_aa = eval(parse(one_char_aa));
		,
		one_aa = parse(one_char_aa);
	);
	one_aa;
);
//---------------------------------------------------------------------
// Now try this out
aa = associative array();
aa["Thing1"] = {14, "String", "z-value"};
aa["Thing2"] = {"DifferentString", 28, "other-z-value"};

// Convert the associative array to a char
char_aa = char(aa);
// Save it to a file
save text file("$temp\myfile.ini", char_aa);
write(char_aa);

// Convert it back to an associative array
new_char_aa = load text file("$temp\myfile.ini");
new_aa = char_to_aa(new_char_aa);
print(new_aa);&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 02 Dec 2021 20:06:31 GMT</pubDate>
    <dc:creator>pmroz</dc:creator>
    <dc:date>2021-12-02T20:06:31Z</dc:date>
    <item>
      <title>Anyone have a good JSL .ini file reader?</title>
      <link>https://community.jmp.com/t5/Discussions/Anyone-have-a-good-JSL-ini-file-reader/m-p/441331#M68969</link>
      <description>&lt;P&gt;I'm imagining&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;[Thing1]
x = 14
y = String
z = ...

[Thing2]
x=DifferentString
y=28&lt;/PRE&gt;
&lt;P&gt;becoming&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;[
	"Thing1"=&amp;gt;
		["x"=&amp;gt;14, //not sure if this should be a number or string
		"y"=&amp;gt;"String", 
		"z"=&amp;gt;"..."], 
	"Thing2"=&amp;gt;
		["x"=&amp;gt;"DifferentString", 
		"y"=&amp;gt;28]
]&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 09 Jun 2023 18:06:26 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Anyone-have-a-good-JSL-ini-file-reader/m-p/441331#M68969</guid>
      <dc:creator>vince_faller</dc:creator>
      <dc:date>2023-06-09T18:06:26Z</dc:date>
    </item>
    <item>
      <title>Re: Anyone have a good JSL .ini file reader?</title>
      <link>https://community.jmp.com/t5/Discussions/Anyone-have-a-good-JSL-ini-file-reader/m-p/441552#M68977</link>
      <description>&lt;P&gt;I think I did something like this when I wanted to use same config file in python and JMP, but gave up dont remember why. But I had to do some parsing for Minitab MAC files and the same idea might work here.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below is modified script to fit this type of strings. Might require adding Trim Whitespace:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1);
config_file = "[Thing1]
x = 14
y = String
z = ...

[Thing2]
x=DifferentString
y=28
";

aa_ini = Associative Array();
cur_key = "";
For Each({line}, Words(config_file, "\!N"),
	If(Ends With(line, "]") &amp;amp; Starts With(line, "["),
		new_key = Word(1, line, "[]");
		aa_ini[Word(1, line, "[]")] = Associative Array();
		cur_key = new_key;
	,
		{key, value} = Words(line, "=");
		//string conversion.. expect everything to be numbers and then try to convert
		new_value = If(IsMissing(num(value)),
			value,
			num(value)
		);
		aa_ini[cur_key][key] = new_value;
	);
);
show(aa_ini);&lt;BR /&gt;&lt;BR /&gt;aa_ini = ["Thing1" =&amp;gt; ["x " =&amp;gt; 14, "y " =&amp;gt; " String", "z " =&amp;gt; " ..."], "Thing2" =&amp;gt; ["x" =&amp;gt; "DifferentString", "y" =&amp;gt; 28]];&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 02 Dec 2021 06:07:54 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Anyone-have-a-good-JSL-ini-file-reader/m-p/441552#M68977</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2021-12-02T06:07:54Z</dc:date>
    </item>
    <item>
      <title>Re: Anyone have a good JSL .ini file reader?</title>
      <link>https://community.jmp.com/t5/Discussions/Anyone-have-a-good-JSL-ini-file-reader/m-p/441596#M68983</link>
      <description>&lt;P&gt;nice! It does need trim for key and value.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;config_file =
"[Thing1]
x = 14
y = String
z = ...

[Thing2]
x=DifferentString with space
y=28
";

aa_ini = Associative Array();
cur_key = "";
For Each( {line}, Words( config_file, "\!N" ),
    If( Ends With( line, "]" ) &amp;amp; Starts With( line, "[" ),
        new_key = Word( 1, line, "[]" );
        aa_ini[Word( 1, line, "[]" )] = Associative Array();
        cur_key = new_key;
    ,
        {key, value} = Words( line, "=" );
		//string conversion.. expect everything to be numbers and then try to convert
        new_value = If( Is Missing( Num( value ) ),
            Trim( value ),
            Num( value )
        );
        aa_ini[cur_key][Trim( key )] = new_value;
    )
);

Write( As JSON Expr( aa_ini ) );&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;CODE class=" language-jsl"&gt;&amp;nbsp;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Move those .ini files into the current century!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;{"Thing1":{"x":14,"y":"String","z":"..."},"Thing2":{"x":"DifferentString with space","y":28}}&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sometimes it is hard to tell if JSON is a step forward or backwards.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Dec 2021 08:57:25 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Anyone-have-a-good-JSL-ini-file-reader/m-p/441596#M68983</guid>
      <dc:creator>Craige_Hales</dc:creator>
      <dc:date>2021-12-02T08:57:25Z</dc:date>
    </item>
    <item>
      <title>Re: Anyone have a good JSL .ini file reader?</title>
      <link>https://community.jmp.com/t5/Discussions/Anyone-have-a-good-JSL-ini-file-reader/m-p/441729#M69005</link>
      <description>&lt;P&gt;I prefer ini/config files over JSON in simple cases because they are usually easier to read and edit directly from the file (be this either good or bad thing) and when I did work with LabVIEW the format got familiar &lt;A href="https://zone.ni.com/reference/en-XX/help/371361R-01/lvconcepts/fileio_configuration_files/" target="_blank" rel="noopener"&gt;configuration files VI&lt;/A&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Currently thinking how I would handle fairly simple configuration files in few of my scripts. I will have to play around with just using .jsl or json/.ini files.&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);

read_ini_file = function({ini_str}, {Default Local},
	aa_ini = Associative Array();
	cur_key = "";
	For Each({line}, Words(ini_str, "\!N"),
		If(Ends With(line, "]") &amp;amp; Starts With(line, "["),
			new_key = Word(1, line, "[]");
			aa_ini[Word(1, line, "[]")] = Associative Array();
			cur_key = new_key;
		,
			{key, value} = Words(line, "=");
			//string conversion.. expect everything to be numbers and then try to convert
			new_value = If(Is Missing(Num(value)),
				Trim(value),
				Num(value)
			);
			aa_ini[cur_key][Trim(key)] = new_value;
		)
	);
	return(aa_ini);
);

aa_to_ini = function({aa_section}, {Default Local},
	ini_str = "";
	section = "\!N[^key_section^]\!N";
	key_value = "^key^=^value^\!N";
	For Each({{key_section, value_section}}, aa_section,
		ini_str ||= Eval Insert(section);
		For Each({{key, value}}, value_section,
			ini_str ||= Eval Insert(key_value);
		);
	);
	return(Trim Whitespace(ini_str,left));
);

config_file = "[Thing1]
x=14
y=String
z=...

[Thing2]
x=DifferentString with space
y=28
";

aa_test = read_ini_file(config_file);
back_to_ini = aa_to_ini(aa_test);

show(aa_test,back_to_ini,config_file);

//back_to_ini == config_file;
//aa_test == read_ini_file(back_to_ini)
//Save Text File("$TEMP/DeleteMe.txt", back_to_ini);
//Open("$TEMP");

//config_file = Load Text File("$TEMP/DeleteMe.txt");
//aa_test == read_ini_file(back_to_ini);
//aa_test["Thing1"]["x"]&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Dec 2021 16:19:17 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Anyone-have-a-good-JSL-ini-file-reader/m-p/441729#M69005</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2021-12-02T16:19:17Z</dc:date>
    </item>
    <item>
      <title>Re: Anyone have a good JSL .ini file reader?</title>
      <link>https://community.jmp.com/t5/Discussions/Anyone-have-a-good-JSL-ini-file-reader/m-p/441750#M69006</link>
      <description>&lt;P&gt;100% agree than XML and JSON and dumped Associative Arrays are not intended to be edited in notepad. And it does not always make sense to write your own editor, so .INI files still have their place.&lt;/P&gt;</description>
      <pubDate>Thu, 02 Dec 2021 16:37:54 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Anyone-have-a-good-JSL-ini-file-reader/m-p/441750#M69006</guid>
      <dc:creator>Craige_Hales</dc:creator>
      <dc:date>2021-12-02T16:37:54Z</dc:date>
    </item>
    <item>
      <title>Re: Anyone have a good JSL .ini file reader?</title>
      <link>https://community.jmp.com/t5/Discussions/Anyone-have-a-good-JSL-ini-file-reader/m-p/441801#M69010</link>
      <description>&lt;P&gt;I routinely read and write associative arrays to an Oracle database.&amp;nbsp; This is a similar approach for an .ini file:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;/*
Function Name: char_to_aa

Description:    Convert a character string to an associative array
*/
char_to_aa = Function({one_char_aa},

	if (contains(one_char_aa, "Associative Array"),
		one_aa = eval(parse(one_char_aa));
		,
		one_aa = parse(one_char_aa);
	);
	one_aa;
);
//---------------------------------------------------------------------
// Now try this out
aa = associative array();
aa["Thing1"] = {14, "String", "z-value"};
aa["Thing2"] = {"DifferentString", 28, "other-z-value"};

// Convert the associative array to a char
char_aa = char(aa);
// Save it to a file
save text file("$temp\myfile.ini", char_aa);
write(char_aa);

// Convert it back to an associative array
new_char_aa = load text file("$temp\myfile.ini");
new_aa = char_to_aa(new_char_aa);
print(new_aa);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 02 Dec 2021 20:06:31 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Anyone-have-a-good-JSL-ini-file-reader/m-p/441801#M69010</guid>
      <dc:creator>pmroz</dc:creator>
      <dc:date>2021-12-02T20:06:31Z</dc:date>
    </item>
    <item>
      <title>Re: Anyone have a good JSL .ini file reader?</title>
      <link>https://community.jmp.com/t5/Discussions/Anyone-have-a-good-JSL-ini-file-reader/m-p/441803#M69011</link>
      <description>&lt;P&gt;Coolio.&amp;nbsp; Yeah. I'm really just trying to do the exact same thing of consolidating my configs for multilple langugaes/frameworks.&lt;BR /&gt;One uses XML, one YML, one TOML, and then I have python and JSL.&amp;nbsp; I personally like the YML for readability and commenting.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;I realized after I asked this I can't really use INI because I need nesting (though I could just have some naming convention and deal with it on my end)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is super useful still.&amp;nbsp; Thanks all.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Dec 2021 21:01:10 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Anyone-have-a-good-JSL-ini-file-reader/m-p/441803#M69011</guid>
      <dc:creator>vince_faller</dc:creator>
      <dc:date>2021-12-02T21:01:10Z</dc:date>
    </item>
  </channel>
</rss>

