<?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: Inserting additional values to existing keys in associative arrays (data dictionaries in JSL) in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Inserting-additional-values-to-existing-keys-in-associative/m-p/617990#M81747</link>
    <description>&lt;P&gt;I am actually using something more like this, so I guess I am missing an evaluation of the variable:&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 );

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 );
//&amp;nbsp;ex = ["a" =&amp;gt; {a_n}, "b" =&amp;gt; {b_n, b_n}, "c" =&amp;gt; {c_n}];&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 29 Mar 2023 20:52:11 GMT</pubDate>
    <dc:creator>FN</dc:creator>
    <dc:date>2023-03-29T20:52:11Z</dc:date>
    <item>
      <title>Inserting additional values to existing keys in associative arrays (data dictionaries in JSL)</title>
      <link>https://community.jmp.com/t5/Discussions/Inserting-additional-values-to-existing-keys-in-associative/m-p/617857#M81736</link>
      <description>&lt;P&gt;I cannot find the proper method to add new values to existing keys in associative arrays.&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 );

ex = Associative Array();

ex["a"] = {"10"};
ex["b"] = {"20"};
ex["c"] = {"30"};
&lt;BR /&gt;// Add new value to "b"
ex["b"] = {"21"};

show(ex);
//ex = ["a" =&amp;gt; {"10"}, "b" =&amp;gt; {"21"}, "c" =&amp;gt; {"30"}];

// Solution needed
//ex = ["a" =&amp;gt; {"10"}, "b" =&amp;gt; {"20", "21"}, "c" =&amp;gt; {"30"}];&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 09 Jun 2023 16:06:47 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Inserting-additional-values-to-existing-keys-in-associative/m-p/617857#M81736</guid>
      <dc:creator>FN</dc:creator>
      <dc:date>2023-06-09T16:06:47Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting additional values to existing keys in associative arrays (data dictionaries in JSL)</title>
      <link>https://community.jmp.com/t5/Discussions/Inserting-additional-values-to-existing-keys-in-associative/m-p/617868#M81737</link>
      <description>&lt;P&gt;Treat the value &lt;STRONG&gt;ex["b"]&lt;/STRONG&gt; like any other list.&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 );

ex = Associative Array();

ex["a"] = {"10"};
ex["b"] = {"20"};
ex["c"] = {"30"};
// Add new value to "b"
Insert Into( ex["b"], {"21"} );

Show( ex );&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 29 Mar 2023 15:16:39 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Inserting-additional-values-to-existing-keys-in-associative/m-p/617868#M81737</guid>
      <dc:creator>Mark_Bailey</dc:creator>
      <dc:date>2023-03-29T15:16:39Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting additional values to existing keys in associative arrays (data dictionaries in JSL)</title>
      <link>https://community.jmp.com/t5/Discussions/Inserting-additional-values-to-existing-keys-in-associative/m-p/617873#M81738</link>
      <description>&lt;P&gt;Mark's answer is the best. You can also do it like this&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;ex["c"][nitems(ex["c"])+1]="99";&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Mar 2023 15:25:12 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Inserting-additional-values-to-existing-keys-in-associative/m-p/617873#M81738</guid>
      <dc:creator>Craige_Hales</dc:creator>
      <dc:date>2023-03-29T15:25:12Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting additional values to existing keys in associative arrays (data dictionaries in JSL)</title>
      <link>https://community.jmp.com/t5/Discussions/Inserting-additional-values-to-existing-keys-in-associative/m-p/617990#M81747</link>
      <description>&lt;P&gt;I am actually using something more like this, so I guess I am missing an evaluation of the variable:&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 );

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 );
//&amp;nbsp;ex = ["a" =&amp;gt; {a_n}, "b" =&amp;gt; {b_n, b_n}, "c" =&amp;gt; {c_n}];&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Mar 2023 20:52:11 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Inserting-additional-values-to-existing-keys-in-associative/m-p/617990#M81747</guid>
      <dc:creator>FN</dc:creator>
      <dc:date>2023-03-29T20:52:11Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting additional values to existing keys in associative arrays (data dictionaries in JSL)</title>
      <link>https://community.jmp.com/t5/Discussions/Inserting-additional-values-to-existing-keys-in-associative/m-p/617991#M81748</link>
      <description>&lt;P&gt;The definition of list says that they aren't evaluated.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;List: Creates a list of items without evaluating them.&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Just wrap them in evallist()&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 );

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}) );&lt;BR /&gt;// or you could this it seems easier.  
// Insert Into( ex[b_s], b_n) ;&lt;BR /&gt;
Show( ex );&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 29 Mar 2023 21:03:13 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Inserting-additional-values-to-existing-keys-in-associative/m-p/617991#M81748</guid>
      <dc:creator>vince_faller</dc:creator>
      <dc:date>2023-03-29T21:03:13Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting additional values to existing keys in associative arrays (data dictionaries in JSL)</title>
      <link>https://community.jmp.com/t5/Discussions/Inserting-additional-values-to-existing-keys-in-associative/m-p/621791#M82083</link>
      <description>&lt;P&gt;This works, but if a given key is not existing, then I cannot insert new elements into it.&lt;BR /&gt;&lt;BR /&gt;Regardless if the key is already there or not, is there a simple way to either introduce a new key--&amp;gt;value or insert a new value?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Apr 2023 17:49:01 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Inserting-additional-values-to-existing-keys-in-associative/m-p/621791#M82083</guid>
      <dc:creator>FN</dc:creator>
      <dc:date>2023-04-11T17:49:01Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting additional values to existing keys in associative arrays (data dictionaries in JSL)</title>
      <link>https://community.jmp.com/t5/Discussions/Inserting-additional-values-to-existing-keys-in-associative/m-p/621795#M82085</link>
      <description>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.</description>
      <pubDate>Tue, 11 Apr 2023 19:23:00 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Inserting-additional-values-to-existing-keys-in-associative/m-p/621795#M82085</guid>
      <dc:creator>Craige_Hales</dc:creator>
      <dc:date>2023-04-11T19:23:00Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting additional values to existing keys in associative arrays (data dictionaries in JSL)</title>
      <link>https://community.jmp.com/t5/Discussions/Inserting-additional-values-to-existing-keys-in-associative/m-p/621919#M82093</link>
      <description>&lt;P&gt;Complete example, including the &lt;EM&gt;obvious&lt;/EM&gt; way that does &lt;EM&gt;not&lt;/EM&gt; work correctly.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;// I wish the default value for AA's worked correctly with a list for the
// default, but it does not:
aa = [=&amp;gt; {}];
Insert Into( aa["fred"], 1 );
Insert Into( aa["ralph"], 2 );
Show( aa ); // aa = [=&amp;gt; {1, 2}]; ?!?!?!?!

// so you must do an explicit test and create the key/list like this:
aa = [=&amp;gt; ];
For( i = 1, i &amp;lt;= 3, i += 1,
	For Each( {key}, {"fred", "ralph"},
		If( !(aa &amp;lt;&amp;lt; 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" =&amp;gt; {1, 2, 3}, "ralph" =&amp;gt; {1, 2, 3}];
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 12 Apr 2023 13:12:14 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Inserting-additional-values-to-existing-keys-in-associative/m-p/621919#M82093</guid>
      <dc:creator>Craige_Hales</dc:creator>
      <dc:date>2023-04-12T13:12:14Z</dc:date>
    </item>
  </channel>
</rss>

