www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Clearing associative arrays

reply "Martin Fuchs" <martin-fuchs gmx.net> writes:
Is there a direct way to clear the content of associative arrays in one 
step?
I would expect a statement like "arr.clear();", "arr.erase();" or 
"arr.removeall();" to do the work, but couldn't find one.
Instead it seams I would have to use the following wordy an inefficient 
approach:

        foreach(x; arr)
            arr.remove(x); 
Sep 18 2007
next sibling parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
Martin Fuchs wrote:
 Is there a direct way to clear the content of associative arrays in one 
 step?
 I would expect a statement like "arr.clear();", "arr.erase();" or 
 "arr.removeall();" to do the work, but couldn't find one.
 Instead it seams I would have to use the following wordy an inefficient 
 approach:
 
         foreach(x; arr)
             arr.remove(x); 
 
 
"arr = null" or "arr = arr.init" works. http://www.prowiki.org/wiki4d/wiki.cgi?action=edit&id=DocComments/Arrays&section=16 (Check those "Comments" link pages on the doc whenever you don't see something in the doc that you should be there) --bb
Sep 18 2007
parent reply "Martin Fuchs" <martin-fuchs gmx.net> writes:
Thanks.

However your link doesn't work for me - it's using an edit action in the 
Wiki I would have to login before.
How do I register in the Wiki, I could only find a "login" link, but no 
"register" function?

"Bill Baxter" <dnewsgroup billbaxter.com> wrote in message 
news:fcnttu$m99$1 digitalmars.com...
 Martin Fuchs wrote:
 Is there a direct way to clear the content of associative arrays in one 
 step?
 I would expect a statement like "arr.clear();", "arr.erase();" or 
 "arr.removeall();" to do the work, but couldn't find one.
 Instead it seams I would have to use the following wordy an inefficient 
 approach:

         foreach(x; arr)
             arr.remove(x);
"arr = null" or "arr = arr.init" works. http://www.prowiki.org/wiki4d/wiki.cgi?action=edit&id=DocComments/Arrays&section=16 (Check those "Comments" link pages on the doc whenever you don't see something in the doc that you should be there) --bb
Sep 18 2007
parent "Stewart Gordon" <smjg_1998 yahoo.com> writes:
"Martin Fuchs" <martin-fuchs gmx.net> wrote in message 
news:fcnufv$n5i$1 digitalmars.com...
 Thanks.

 However your link doesn't work for me - it's using an edit action in the 
 Wiki I would have to login before.
 How do I register in the Wiki, I could only find a "login" link, but no 
 "register" function?
<snip top of upside-down reply> Wiki4D doesn't have user accounts as such - you just have to give yourself a name in the preferences. Stewart.
Sep 18 2007
prev sibling parent Oskar Linde <oskar.lindeREM OVEgmail.com> writes:
Martin Fuchs wrote:
 Is there a direct way to clear the content of associative arrays in one 
 step?
 I would expect a statement like "arr.clear();", "arr.erase();" or 
 "arr.removeall();" to do the work, but couldn't find one.
 Instead it seams I would have to use the following wordy an inefficient 
 approach:
 
         foreach(x; arr)
             arr.remove(x); 
If you only want to clear the reference, arr = null; will do the trick. Otherwise, you need to resort to non-portable hackery (something like this should definitely be included in the compiler/runtime:) private struct BB { void*[]b; size_t nodes; } private union ToPtr(T) {T x; void * ptr; } void clear(T,E)(T[E] aa) { ToPtr!(typeof(aa)) toptr; toptr.x = aa; BB* b = cast(BB*) toptr.ptr; if (b) { b.b = null; b.nodes = 0; } } use: arr.clear(); Warning: hardly tested, but should work with DMD 1.020/Phobos. Regards, -- Oskar
Sep 18 2007