www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Remove all elements in an associative array

reply Nub Public <nubpublic gmail.com> writes:
Is there a way to remove all elements in an associative array?

I tried clear() but it doesn't work. For some reason it sets the length 
of the array to gibberish. What does clear() do exactly?
Jun 24 2011
next sibling parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On 2011-06-24 20:41, Nub Public wrote:
 Is there a way to remove all elements in an associative array?
 
 I tried clear() but it doesn't work. For some reason it sets the length
 of the array to gibberish. What does clear() do exactly?
clear invokes an object's destructor and puts it in an invalid state. You definitely don't want to do that to an associative array to empty it - if ever. Generally, the simplest thing to do to clear out an AA is to simply create a new one. Either set the existing one to null or assign it a new AA. The old one can then be garbage collected. Now, if you actually need to remove all of the elements from the existing one (e.g. because multiple places in your code have references to it), then the only way that I'm aware of is to remove all of the elements one by one with remove. http://www.d-programming-language.org/hash-map.html - Jonathan M Davis
Jun 24 2011
next sibling parent reply Nub Public <nubpublic gmail.com> writes:
On 6/25/2011 11:54 AM, Jonathan M Davis wrote:
 On 2011-06-24 20:41, Nub Public wrote:
 Is there a way to remove all elements in an associative array?

 I tried clear() but it doesn't work. For some reason it sets the length
 of the array to gibberish. What does clear() do exactly?
clear invokes an object's destructor and puts it in an invalid state. You definitely don't want to do that to an associative array to empty it - if ever. Generally, the simplest thing to do to clear out an AA is to simply create a new one. Either set the existing one to null or assign it a new AA. The old one can then be garbage collected.
Thanks. That was very helpful.
 Now, if you actually need to remove all of
 the elements from the existing one (e.g. because multiple places in your code
 have references to it), then the only way that I'm aware of is to remove all
 of the elements one by one with remove.

 http://www.d-programming-language.org/hash-map.html

 - Jonathan M Davis
How to do this properly? foreach (key, value; aa) remove(key); This doesn't work since it's altering the aa in each iteration.
Jun 24 2011
parent reply Ali =?iso-8859-1?q?=C7ehreli?= <acehreli yahoo.com> writes:
On Sat, 25 Jun 2011 12:57:15 +0800, Nub Public wrote:

 How to do this properly?
 
 foreach (key, value; aa)
    remove(key);
 
 This doesn't work since it's altering the aa in each iteration.
The .keys property returns an array of the keys (as a copy). This should be safe: foreach (key; aa.keys) { aa.remove(key); } Ali
Jun 24 2011
parent Nub Public <nubpublic gmail.com> writes:
On 6/25/2011 1:57 PM, Ali Çehreli wrote:
 On Sat, 25 Jun 2011 12:57:15 +0800, Nub Public wrote:

 How to do this properly?

 foreach (key, value; aa)
     remove(key);

 This doesn't work since it's altering the aa in each iteration.
The .keys property returns an array of the keys (as a copy). This should be safe: foreach (key; aa.keys) { aa.remove(key); } Ali
Thank you.
Jun 24 2011
prev sibling parent bearophile <bearophileHUGS lycos.com> writes:
Jonathan M Davis:

 clear invokes an object's destructor and puts it in an invalid state. You 
 definitely don't want to do that to an associative array to empty it - if 
 ever.
I am sure there's a need for a built-in function (or function in Object) to delete all items of an associative array. It's a basic common need. Bye, bearophile
Jun 25 2011
prev sibling parent Jimmy Cao <jcao219 gmail.com> writes:
On Fri, Jun 24, 2011 at 10:41 PM, Nub Public <nubpublic gmail.com> wrote:

 Is there a way to remove all elements in an associative array?

 I tried clear() but it doesn't work. For some reason it sets the length of
 the array to gibberish. What does clear() do exactly?
This same thing was asked many years ago: http://dsource.org/projects/tutorials/wiki/DeleteAllKeysFromAssocArrayExample The example is very old and it's for D1.
Jun 24 2011