www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Remove all elements in an associative array

reply Nub Public <nubpublic gmail.com> writes:
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?
Jun 24 2011
next sibling parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
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
Jun 24 2011
next sibling parent reply Nub Public <nubpublic gmail.com> writes:
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.
Jun 24 2011
parent Nub Public <nubpublic gmail.com> writes:
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.
Jun 24 2011
prev sibling parent bearophile <bearophileHUGS lycos.com> writes:
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
Jun 25 2011
prev sibling next sibling parent Ali =?iso-8859-1?q?=C7ehreli?= <acehreli yahoo.com> writes:
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
Jun 24 2011
prev sibling parent Jimmy Cao <jcao219 gmail.com> writes:
--001485f80f3430ad9604a683988b
Content-Type: text/plain; charset=ISO-8859-1

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. --001485f80f3430ad9604a683988b Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On Fri, Jun 24, 2011 at 10:41 PM, Nub Public <span dir=3D"ltr">&lt;<a href= =3D"mailto:nubpublic gmail.com">nubpublic gmail.com</a>&gt;</span> wrote:<b= r><div class=3D"gmail_quote"><blockquote class=3D"gmail_quote" style=3D"mar= gin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"> Is there a way to remove all elements in an associative array?<br> <br> I tried clear() but it doesn&#39;t work. For some reason it sets the length= of the array to gibberish. What does clear() do exactly?<br> </blockquote></div><br><div><br></div><div>This same thing was asked many y= ears ago:</div><div><a href=3D"http://dsource.org/projects/tutorials/wiki/D= eleteAllKeysFromAssocArrayExample">http://dsource.org/projects/tutorials/wi= ki/DeleteAllKeysFromAssocArrayExample</a></div> <div><br></div><div>The example is very old and it&#39;s for D1.</div> --001485f80f3430ad9604a683988b--
Jun 24 2011