Thread overview
Remove all elements in an associative array
Jun 25, 2011
Nub Public
Jun 25, 2011
Jonathan M Davis
Jun 25, 2011
Nub Public
Jun 25, 2011
Ali Çehreli
Jun 25, 2011
Nub Public
Jun 25, 2011
bearophile
Jun 25, 2011
Jimmy Cao
June 25, 2011
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?
June 25, 2011
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
June 25, 2011
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.
June 25, 2011
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
June 25, 2011
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.
June 25, 2011
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.


June 25, 2011
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