www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How i can clear Associative Arrays

reply "gedaiu" <szabobogdan yahoo.com> writes:
Hi,

I have an associative array: string values[string], and i want to 
remove all the values from this array. I looked at the 
documentation here: http://dlang.org/hash-map.html and i can't 
see any method for this action.

There is a nice way to remove the values, or I should use foreach?

Thanks!
Apr 13 2013
parent reply "Nicolas Guillemot" <nlguillemot gmail.com> writes:
Hey gedaiu,

I'm still a novice to D, but here are some solutions I found. 
They can probably be improved.

1) Assigning to it an empty map
https://ideone.com/h7ffmD

2) Removing all entries
https://ideone.com/E7k2WL

My guess is that the first method is more efficient. I wish I 
knew how to do it without having to explicitly declare the 
"empty" variable.

Cheers.
Apr 13 2013
parent reply "gedaiu" <szabobogdan yahoo.com> writes:
On Saturday, 13 April 2013 at 09:09:36 UTC, Nicolas Guillemot 
wrote:
 Hey gedaiu,

 I'm still a novice to D, but here are some solutions I found. 
 They can probably be improved.

 1) Assigning to it an empty map
 https://ideone.com/h7ffmD

 2) Removing all entries
 https://ideone.com/E7k2WL

 My guess is that the first method is more efficient. I wish I 
 knew how to do it without having to explicitly declare the 
 "empty" variable.

 Cheers.
Hi, string[string] empty; values = empty; looks great, but i cleared the array like this: values = null; and it seems it works great. But, i don't know how correct is this... I was expecting to have a clear() method on array. Thanks, Bogdan
Apr 13 2013
next sibling parent "Nicolas Guillemot" <nlguillemot gmail.com> writes:
 values = null;

 and it seems it works great. But, i don't know how correct is 
 this... I was expecting to have a clear() method on array.
Makes sense to me! Looks like the technique you described is explained here: http://ddili.org/ders/d.en/null_is.html Good find, thanks for sharing!
Apr 13 2013
prev sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 4/13/13, gedaiu <szabobogdan yahoo.com> wrote:
 looks great, but i cleared the array like this:

 values = null;
That's not clearing the array, that's clearing the reference to the array. For example: void main() { int[int] hash; hash[1] = 1; auto hash2 = hash; // new reference hash = null; // clear the reference assert(1 in hash2); // hash still exists }
Apr 13 2013
parent reply "gedaiu" <szabobogdan yahoo.com> writes:
On Saturday, 13 April 2013 at 09:52:45 UTC, Andrej Mitrovic wrote:
 On 4/13/13, gedaiu <szabobogdan yahoo.com> wrote:
 looks great, but i cleared the array like this:

 values = null;
That's not clearing the array, that's clearing the reference to the array. For example: void main() { int[int] hash; hash[1] = 1; auto hash2 = hash; // new reference hash = null; // clear the reference assert(1 in hash2); // hash still exists }
I know, that's why I am asking how i should do this...
Apr 13 2013
parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 4/13/13, gedaiu <szabobogdan yahoo.com> wrote:
 I know, that's why I am asking how i should do this...
I think we should introduce a removeAll function for hashes. Either through Druntime or through a UFCS function that we could put in std.array or somewhere. Putting it in Druntime is probably the most efficient way.
Apr 13 2013
parent reply "Nicolas Guillemot" <nlguillemot gmail.com> writes:
 I think we should introduce a removeAll function for hashes. 
 Either
 through Druntime or through a UFCS function that we could put in
 std.array or somewhere.
How about .clear() for consistency with C++ containers?
Apr 13 2013
parent reply "gedaiu" <szabobogdan yahoo.com> writes:
On Saturday, 13 April 2013 at 21:10:16 UTC, Nicolas Guillemot 
wrote:
 I think we should introduce a removeAll function for hashes. 
 Either
 through Druntime or through a UFCS function that we could put 
 in
 std.array or somewhere.
How about .clear() for consistency with C++ containers?
It looks nice... i am waiting for it :P
Apr 13 2013
next sibling parent reply "Andrea Fontana" <nospam example.com> writes:
On Sunday, 14 April 2013 at 06:50:08 UTC, gedaiu wrote:
 On Saturday, 13 April 2013 at 21:10:16 UTC, Nicolas Guillemot 
 wrote:
 I think we should introduce a removeAll function for hashes. 
 Either
 through Druntime or through a UFCS function that we could put 
 in
 std.array or somewhere.
How about .clear() for consistency with C++ containers?
It looks nice... i am waiting for it :P
If you can't do myArray = null, you can define this: void removeAll(T, K)(T[K] arr) { foreach(k; arr.keys) arr.remove(k); } or this: import std.traits; void removeAll(T)(T arr) if (isAssociativeArray!T) { foreach(k; arr.keys) arr.remove(k); } and then using UCFS you can write: string[int] test; test[10] = "hello"; test[20] = "world"; test.writeln; test.removeAll; test.writeln;
Apr 15 2013
parent reply "Tobias Pankrath" <tobias pankrath.net> writes:
You could set the length to 0.
Apr 15 2013
next sibling parent "Andrea Fontana" <nospam example.com> writes:
Not on associative arrays afaik.

On Monday, 15 April 2013 at 11:36:55 UTC, Tobias Pankrath wrote:
 You could set the length to 0.
Apr 15 2013
prev sibling parent "Michael" <pr m1xa.com> writes:
On Monday, 15 April 2013 at 11:36:55 UTC, Tobias Pankrath wrote:
 You could set the length to 0.
Collection.length = 0; Later this method was included to BCL. In D length property for AA is readonly.
Apr 15 2013
prev sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Sun, 14 Apr 2013 02:50:07 -0400, gedaiu <szabobogdan yahoo.com> wrote:

 On Saturday, 13 April 2013 at 21:10:16 UTC, Nicolas Guillemot wrote:
 I think we should introduce a removeAll function for hashes. Either
 through Druntime or through a UFCS function that we could put in
 std.array or somewhere.
How about .clear() for consistency with C++ containers?
It looks nice... i am waiting for it :P
Might be confusing. clear used to do what destroy does now and is still in TDPL. -Steve
Apr 15 2013
parent Marco Leise <Marco.Leise gmx.de> writes:
Am Mon, 15 Apr 2013 13:06:01 -0400
schrieb "Steven Schveighoffer" <schveiguy yahoo.com>:

 On Sun, 14 Apr 2013 02:50:07 -0400, gedaiu <szabobogdan yahoo.com> wrote:
 
 On Saturday, 13 April 2013 at 21:10:16 UTC, Nicolas Guillemot wrote:
 I think we should introduce a removeAll function for hashes. Either
 through Druntime or through a UFCS function that we could put in
 std.array or somewhere.
How about .clear() for consistency with C++ containers?
It looks nice... i am waiting for it :P
Might be confusing. clear used to do what destroy does now and is still in TDPL. -Steve
Right, but the reason clear was renamed to destroy in the first place was to remove this ambiguity. When I was new to D I used clear() on AAs ignorant to the fact that it is not meant to clear containers as in other languages. Despite the issue with TDPL I'm looking forward to clear() as a container method. -- Marco
Apr 28 2013