Remove all elements in an associative array

Ali Çehreli acehreli at yahoo.com
Fri Jun 24 22:57:13 PDT 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


More information about the Digitalmars-d-learn mailing list