Thread overview | |||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
April 13, 2013 How i can clear Associative Arrays | ||||
---|---|---|---|---|
| ||||
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! |
April 13, 2013 Re: How i can clear Associative Arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to gedaiu | 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. |
April 13, 2013 Re: How i can clear Associative Arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nicolas Guillemot | 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
|
April 13, 2013 Re: How i can clear Associative Arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to gedaiu | > 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! |
April 13, 2013 Re: How i can clear Associative Arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to gedaiu | 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
}
|
April 13, 2013 Re: How i can clear Associative Arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | 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...
|
April 13, 2013 Re: How i can clear Associative Arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to gedaiu | 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.
|
April 13, 2013 Re: How i can clear Associative Arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | > 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?
|
April 14, 2013 Re: How i can clear Associative Arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nicolas Guillemot | 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
|
April 15, 2013 Re: How i can clear Associative Arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to gedaiu | 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;
|
Copyright © 1999-2021 by the D Language Foundation