AA strange behavior

Oskar Linde oskar.lindeREM at OVEgmail.com
Mon Jun 12 09:23:01 PDT 2006


marc michel skrev:
> I found a strange behavior using AAs :

[snip]

> foreach( char[]s, int[] i; aa ) {
> aa.remove(s);
> writefln("\"%s\" => removed ",s);
> }

You may not delete aa elements within a foreach loop. The foreach 
iterator will be confused.

> The only workaround is to add a "while( aa.length > 0 ) " surroundind the
> foreach loop which does aa.remove().

There are other ways. For instance:

foreach(key;aa.keys)
	aa.remove(key);

or faster (as you seem to want to delete all elements), but ugly, 
hackish and undocumented:

struct BB { void *[] buckets; size_t nodes; }

...

(cast(BB*)&aa).buckets = null;
(cast(BB*)&aa).nodes = 0;

(A wish would be for the above to be implemented as aa.clear())

Or if you just want to forget about your current aa instance:

aa = null;

/Oskar



More information about the Digitalmars-d-learn mailing list