digitalmars.D.learn - How i can clear Associative Arrays
- gedaiu (7/7) Apr 13 2013 Hi,
- Nicolas Guillemot (11/11) Apr 13 2013 Hey gedaiu,
- gedaiu (11/22) Apr 13 2013 Hi,
- Nicolas Guillemot (3/6) Apr 13 2013 Makes sense to me! Looks like the technique you described is
- Andrej Mitrovic (11/13) Apr 13 2013 That's not clearing the array, that's clearing the reference to the
- gedaiu (2/17) Apr 13 2013 I know, that's why I am asking how i should do this...
- Andrej Mitrovic (5/6) Apr 13 2013 I think we should introduce a removeAll function for hashes. Either
- Nicolas Guillemot (1/5) Apr 13 2013 How about .clear() for consistency with C++ containers?
- gedaiu (3/9) Apr 13 2013 It looks nice... i am waiting for it :P
- Andrea Fontana (21/31) Apr 15 2013 If you can't do myArray = null, you can define this:
- Tobias Pankrath (1/1) Apr 15 2013 You could set the length to 0.
- Andrea Fontana (2/3) Apr 15 2013
- Michael (4/5) Apr 15 2013 Before version 4.0 in C# a Clear extension-method just calls
- Steven Schveighoffer (4/11) Apr 15 2013 Might be confusing. clear used to do what destroy does now and is still...
- Marco Leise (10/25) Apr 28 2013 Right, but the reason clear was renamed to destroy in the
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
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
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
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
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
On Saturday, 13 April 2013 at 09:52:45 UTC, Andrej Mitrovic wrote:On 4/13/13, gedaiu <szabobogdan yahoo.com> wrote:I know, that's why I am asking how i should do this...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
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
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
On Saturday, 13 April 2013 at 21:10:16 UTC, Nicolas Guillemot wrote:It looks nice... i am waiting for it :PI 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
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: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;It looks nice... i am waiting for it :PI 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 15 2013
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
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
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:Might be confusing. clear used to do what destroy does now and is still in TDPL. -SteveIt looks nice... i am waiting for it :PI 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 15 2013
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: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. -- MarcoOn Saturday, 13 April 2013 at 21:10:16 UTC, Nicolas Guillemot wrote:Might be confusing. clear used to do what destroy does now and is still in TDPL. -SteveIt looks nice... i am waiting for it :PI 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 28 2013