www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Insert an element into an Associative Array ?

reply "Chris Pons" <cmpons gmail.com> writes:
I'm playing around with associative arrays right now and I can't
seem to figure out how to add additional objects to the array. I
tried insert but it doesn't recognize both arguments.

Also, if I do this it produces an error:

Node[bool] test;

Node node;

Node[bool] temp = [ false:node ];

test ~= temp;


Error	1	Error: cannot append type Node[bool] to type
Node[bool]	C:\Users\CP\Documents\Visual Studio
2010\Projects\D\STDS\NPC.d	256	

Does this mean you can't use the append operator on associative
arrays ? ( this one ~= ) ?
Apr 04 2012
next sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Wed, Apr 04, 2012 at 08:57:54PM +0200, Chris Pons wrote:
 I'm playing around with associative arrays right now and I can't
 seem to figure out how to add additional objects to the array. I
 tried insert but it doesn't recognize both arguments.
 
 Also, if I do this it produces an error:
 
 Node[bool] test;
 
 Node node;
 
 Node[bool] temp = [ false:node ];
 
 test ~= temp;
Try: temp[false] = node; T -- One Word to write them all, One Access to find them, One Excel to count them all, And thus to Windows bind them. -- Mike Champion
Apr 04 2012
prev sibling next sibling parent Jacob Carlborg <doob me.com> writes:
On 2012-04-04 20:57, Chris Pons wrote:
 I'm playing around with associative arrays right now and I can't
 seem to figure out how to add additional objects to the array. I
 tried insert but it doesn't recognize both arguments.

 Also, if I do this it produces an error:

 Node[bool] test;

 Node node;

 Node[bool] temp = [ false:node ];

 test ~= temp;


 Error 1 Error: cannot append type Node[bool] to type
 Node[bool] C:\Users\CP\Documents\Visual Studio
 2010\Projects\D\STDS\NPC.d 256

 Does this mean you can't use the append operator on associative
 arrays ? ( this one ~= ) ?
Yes, since for an associative array you need to add both a key and a value. The syntax used is this: aa[key] = value; i.e. test[false] = node; -- /Jacob Carlborg
Apr 04 2012
prev sibling next sibling parent reply "ixid" <nuaccount gmail.com> writes:
You're trying to append to something that's not an array, you 
haven't set the value part to be an array so you're doing doing 
the same as:

int a;
a ~= 5;

which obviously doesn't work. If you want to append to an 
associative array create it like this:

Node[][bool] test; //Conceptually maybe clearer as Node[] 
test[bool]
Node node;
test[false] ~= node;

Or set them explicitly as other posters have explained if you 
don't want an array of values at a given key.
Apr 04 2012
parent reply "ixid" <nuaccount gmail.com> writes:
Oops, the comment should read as follows, and test[bool] should 
not be on the next line.

//Conceptually maybe clearer as Node[] test[bool]
Apr 04 2012
parent "Chris Pons" <cmpons gmail.com> writes:
Ok, thanks for the help, much appreciated.

On Wednesday, 4 April 2012 at 19:57:40 UTC, ixid wrote:
 Oops, the comment should read as follows, and test[bool] should 
 not be on the next line.

 //Conceptually maybe clearer as Node[] test[bool]
Apr 04 2012
prev sibling parent reply Ary Manzana <ary esperanto.org.ar> writes:
On 4/5/12 2:57 AM, Chris Pons wrote:
 I'm playing around with associative arrays right now and I can't
 seem to figure out how to add additional objects to the array. I
 tried insert but it doesn't recognize both arguments.

 Also, if I do this it produces an error:

 Node[bool] test;

 Node node;

 Node[bool] temp = [ false:node ];

 test ~= temp;


 Error 1 Error: cannot append type Node[bool] to type
 Node[bool] C:\Users\CP\Documents\Visual Studio
 2010\Projects\D\STDS\NPC.d 256

 Does this mean you can't use the append operator on associative
 arrays ? ( this one ~= ) ?
By the way, why is it called "associative array"? A name like Hash or Map would be much better. Everyone knows what a Hash means. I don't see anyone using "associative array" to refer to a Hash. And I think this is the source of the confusion Chris has... I mean, you can't append to an associate array. What part of it makes it an "array"?
Apr 05 2012
parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Friday, April 06, 2012 11:46:24 Ary Manzana wrote:
 By the way, why is it called "associative array"? A name like Hash or
 Map would be much better. Everyone knows what a Hash means. I don't see
 anyone using "associative array" to refer to a Hash. And I think this is
 the source of the confusion Chris has...
 
 I mean, you can't append to an associate array. What part of it makes it
 an "array"?
The term associative array is quite correct: http://en.wikipedia.org/wiki/Associative_array Also, static arrays are arrays, but you can't append to them. And most programming languages with arrays don't have any form of array concatenation (certainly, among C-based languages, D is the only one that I'm aware of). They do with strings, but strings generally aren't arrays in such languages. So, the fact that something is an array says _nothing_ about whether you can append to it. The fact that you can with D's dynamic arrays is a fantastic feature, but it is by no means guaranteed simply because they're arrays. And the very nature of an associative array pretty clearly doesn't make any sense with appending, since appending doesn't involve a key-value pair at all, so I'm honestly very surprised to see that anyone would make that mistake. - Jonathan M Davis
Apr 05 2012