Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
June 25, 2011 Remove all elements in an associative array | ||||
---|---|---|---|---|
| ||||
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 Re: Remove all elements in an associative array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nub Public | 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 Re: Remove all elements in an associative array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | 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 Re: Remove all elements in an associative array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nub Public | 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 Re: Remove all elements in an associative array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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 Re: Remove all elements in an associative array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nub Public Attachments:
| 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 Re: Remove all elements in an associative array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | 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
|
Copyright © 1999-2021 by the D Language Foundation