I'm imagining
[Thing1] x = 14 y = String z = ... [Thing2] x=DifferentString y=28
becoming
[ "Thing1"=> ["x"=>14, //not sure if this should be a number or string "y"=>"String", "z"=>"..."], "Thing2"=> ["x"=>"DifferentString", "y"=>28] ]
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.
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.
Below is modified script to fit this type of strings. Might require adding Trim Whitespace:
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, "]") & 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);<BR /><BR />aa_ini = ["Thing1" => ["x " => 14, "y " => " String", "z " => " ..."], "Thing2" => ["x" => "DifferentString", "y" => 28]];
nice! It does need trim for key and value.
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, "]" ) & 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 ) );
Move those .ini files into the current century!
{"Thing1":{"x":14,"y":"String","z":"..."},"Thing2":{"x":"DifferentString with space","y":28}}
Sometimes it is hard to tell if JSON is a step forward or backwards.
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 configuration files VI.
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.
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, "]") & 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"]
Coolio. Yeah. I'm really just trying to do the exact same thing of consolidating my configs for multilple langugaes/frameworks.One uses XML, one YML, one TOML, and then I have python and JSL. I personally like the YML for readability and commenting. 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)
This is super useful still. Thanks all.