digitalmars.D.learn - Remove all elements in an associative array
- Nub Public (3/3) Jun 24 2011 Is there a way to remove all elements in an associative array?
- Jonathan M Davis (12/16) Jun 24 2011 clear invokes an object's destructor and puts it in an invalid state. Yo...
- Nub Public (6/23) Jun 24 2011 How to do this properly?
- Ali =?iso-8859-1?q?=C7ehreli?= (7/13) Jun 24 2011 The .keys property returns an array of the keys (as a copy). This should...
- Nub Public (2/15) Jun 24 2011 Thank you.
- bearophile (4/7) Jun 25 2011 I am sure there's a need for a built-in function (or function in Object)...
- Jimmy Cao (4/7) Jun 24 2011 This same thing was asked many years ago:
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
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
On 6/25/2011 11:54 AM, Jonathan M Davis wrote:On 2011-06-24 20:41, Nub Public wrote:Thanks. That was very helpful.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 DavisHow 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
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
On 6/25/2011 1:57 PM, Ali Çehreli wrote:On Sat, 25 Jun 2011 12:57:15 +0800, Nub Public wrote:Thank you.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
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
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.
Jun 24 2011