Re: Inserting additional values to existing keys in associative arrays (data dictionaries in JSL)Treat the value ex["b"] like any other list.
Names Default To Here( 1 );
ex = Associative Array();
ex["a"] = {"10"};
ex["b"] = {"20"};
ex["c"] = {"30"};
// Add new value to "b"
Insert Into( ex["b"], {"21"} );
Show( ex );Re: Inserting additional values to existing keys in associative arrays (data dictionaries in JSL)Mark's answer is the best. You can also do it like this
ex["c"][nitems(ex["c"])+1]="99";
which is uglier, slower, and less concise. But it might suggest other possibilities. Lists have a cool subscripting property for one-beyond-the-end that allows extending the list by one element.
Re: Inserting additional values to existing keys in associative arrays (data dictionaries in JSL)I am actually using something more like this, so I guess I am missing an evaluation of the variable:
Names Default To Here( 1 );
ex = Associative Array();
a_s = "a";
b_s = "b";
c_s = "c";
a_n = "10";
b_n = "20";
c_n = "30";
ex[a_s] = {a_n};
ex[b_s] = {b_n};
ex[c_s] = {c_n};
// Add new value to "b"
b_n = "21";
Insert Into( ex[b_s], {b_n} );
Show( ex );
// ex = ["a" => {a_n}, "b" => {b_n, b_n}, "c" => {c_n}];
Re: Inserting additional values to existing keys in associative arrays (data dictionaries in JSL)The definition of list says that they aren't evaluated.
List: Creates a list of items without evaluating them.
Just wrap them in evallist()
Names Default To Here( 1 );
ex = Associative Array();
a_s = "a";
b_s = "b";
c_s = "c";
a_n = "10";
b_n = "20";
c_n = "30";
ex[a_s] = Evallist({a_n});
ex[b_s] = Evallist({b_n});
ex[c_s] = Evallist({c_n});
// Add new value to "b"
b_n = "21";
Insert Into( ex[b_s], Evallist({b_n}) );<BR />// or you could this it seems easier.
// Insert Into( ex[b_s], b_n) ;<BR />
Show( ex );Re: Inserting additional values to existing keys in associative arrays (data dictionaries in JSL)This works, but if a given key is not existing, then I cannot insert new elements into it.
Regardless if the key is already there or not, is there a simple way to either introduce a new key-->value or insert a new value?
Re: Inserting additional values to existing keys in associative arrays (data dictionaries in JSL)I think you'll need to use an if statement to check if the key exists and create it if it doesn't. I usually do this by creating it with an empty list, then fall into the code that adds the new element to the existing list.Re: Inserting additional values to existing keys in associative arrays (data dictionaries in JSL)Complete example, including the obvious way that does not work correctly.
// I wish the default value for AA's worked correctly with a list for the
// default, but it does not:
aa = [=> {}];
Insert Into( aa["fred"], 1 );
Insert Into( aa["ralph"], 2 );
Show( aa ); // aa = [=> {1, 2}]; ?!?!?!?!
// so you must do an explicit test and create the key/list like this:
aa = [=> ];
For( i = 1, i <= 3, i += 1,
For Each( {key}, {"fred", "ralph"},
If( !(aa << Contains( key )), // key does not exist?
aa[key] = {};// add the key with an empty list
);
Insert Into( aa[key], i );// insert into list
)
);
Show( aa ); // aa = ["fred" => {1, 2, 3}, "ralph" => {1, 2, 3}];
Inserting additional values to existing keys in associative arrays (data dictionaries in JSL)I cannot find the proper method to add new values to existing keys in associative arrays.
Names Default To Here( 1 );
ex = Associative Array();
ex["a"] = {"10"};
ex["b"] = {"20"};
ex["c"] = {"30"};
<BR />// Add new value to "b"
ex["b"] = {"21"};
show(ex);
//ex = ["a" => {"10"}, "b" => {"21"}, "c" => {"30"}];
// Solution needed
//ex = ["a" => {"10"}, "b" => {"20", "21"}, "c" => {"30"}];Advanced Statistical ModelingData Blending and Cleanup