Remove all elements in an associative array

Nub Public nubpublic at gmail.com
Fri Jun 24 23:23:56 PDT 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.


More information about the Digitalmars-d-learn mailing list