Thread overview | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
November 06, 2013 How to re-initialise an associative array. | ||||
---|---|---|---|---|
| ||||
A simple request but i'm failing hard. How do i re-init an associative array? This is obviously not the way: import std.stdio; void main(string[] args) { int[string] x; x["hello"] = 1; x["world"] = 2; writefln("%s", x); x = new int[string]; writefln("%s", x); // Should be empty. } |
November 06, 2013 Re: How to re-initialise an associative array. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gary Willoughby | On Wednesday, 6 November 2013 at 16:15:36 UTC, Gary Willoughby wrote:
> A simple request but i'm failing hard. How do i re-init an associative array? This is obviously not the way:
>
> import std.stdio;
>
> void main(string[] args)
> {
> int[string] x;
>
> x["hello"] = 1;
> x["world"] = 2;
>
> writefln("%s", x);
>
> x = new int[string];
>
> writefln("%s", x); // Should be empty.
> }
Maybe there's a neater way but x = x.init works.
|
November 06, 2013 Re: How to re-initialise an associative array. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gary Willoughby | On Wednesday, 6 November 2013 at 16:15:36 UTC, Gary Willoughby wrote:
> A simple request but i'm failing hard. How do i re-init an associative array? This is obviously not the way:
>
> import std.stdio;
>
> void main(string[] args)
> {
> int[string] x;
>
> x["hello"] = 1;
> x["world"] = 2;
>
> writefln("%s", x);
>
> x = new int[string];
>
> writefln("%s", x); // Should be empty.
> }
x.clear();
|
November 06, 2013 Re: How to re-initialise an associative array. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Davidson | On Wednesday, 6 November 2013 at 16:34:13 UTC, Daniel Davidson wrote: > On Wednesday, 6 November 2013 at 16:15:36 UTC, Gary Willoughby wrote: >> A simple request but i'm failing hard. How do i re-init an associative array? This is obviously not the way: >> >> import std.stdio; >> >> void main(string[] args) >> { >> int[string] x; >> >> x["hello"] = 1; >> x["world"] = 2; >> >> writefln("%s", x); >> >> x = new int[string]; >> >> writefln("%s", x); // Should be empty. >> } > > x.clear(); I looked at that but apparently it leaves the array in an unsafe state. Source: http://forum.dlang.org/thread/iu3ll6$2d48$1@digitalmars.com |
November 06, 2013 Re: How to re-initialise an associative array. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gary Willoughby | On Wednesday, 6 November 2013 at 16:41:19 UTC, Gary Willoughby wrote:
>> x.clear();
>
> I looked at that but apparently it leaves the array in an unsafe state.
>
> Source: http://forum.dlang.org/thread/iu3ll6$2d48$1@digitalmars.com
Wow! Good to know, thanks!
|
November 06, 2013 Re: How to re-initialise an associative array. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gary Willoughby | On Wednesday, 6 November 2013 at 16:41:19 UTC, Gary Willoughby wrote: > > I looked at that but apparently it leaves the array in an unsafe state. > > Source: http://forum.dlang.org/thread/iu3ll6$2d48$1@digitalmars.com Is that still the case? The following seems to work just fine. Maybe Kenji has been working his magic :-) ? import std.stdio; void main() { string[string] foo = ["a" : "a", "b" : "B"]; writeln(foo); foo.clear(); writeln(foo); writeln(foo.length); foo["x"] = "this is a test"; writeln(foo); writeln(foo.length); } ----------------------------------------- ["a":"a", "b":"B"] [] 0 ["x":"this is a test"] 1 |
November 06, 2013 Re: How to re-initialise an associative array. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Davidson | On Wednesday, 6 November 2013 at 16:49:44 UTC, Daniel Davidson wrote:
> On Wednesday, 6 November 2013 at 16:41:19 UTC, Gary Willoughby wrote:
>>
>> I looked at that but apparently it leaves the array in an unsafe state.
>>
>> Source: http://forum.dlang.org/thread/iu3ll6$2d48$1@digitalmars.com
>
> Is that still the case? The following seems to work just fine. Maybe Kenji has been working his magic :-) ?
>
> import std.stdio;
>
> void main() {
> string[string] foo = ["a" : "a", "b" : "B"];
> writeln(foo);
> foo.clear();
> writeln(foo);
> writeln(foo.length);
> foo["x"] = "this is a test";
> writeln(foo);
> writeln(foo.length);
> }
> -----------------------------------------
> ["a":"a", "b":"B"]
> []
> 0
> ["x":"this is a test"]
> 1
I'm not taking the chance and currently using:
x = (int[string]).init;
|
November 06, 2013 Re: How to re-initialise an associative array. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gary Willoughby | On Wed, Nov 06, 2013 at 05:15:34PM +0100, Gary Willoughby wrote: > A simple request but i'm failing hard. How do i re-init an associative array? [...] Just assign null to it: import std.stdio; void main() { int[string] aa; aa["a"] = 1; aa["b"] = 2; writeln(aa); aa = null; aa["a"] = 2; aa["b"] = 1; writeln(aa); } Output: ["a":1, "b":2] ["a":2, "b":1] The GC will take care of cleaning up the old data. T -- Don't get stuck in a closet---wear yourself out. |
November 06, 2013 Re: How to re-initialise an associative array. | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | On Wednesday, 6 November 2013 at 17:49:41 UTC, H. S. Teoh wrote:
>
> The GC will take care of cleaning up the old data.
>
>
> T
Unfortunately it will not take care of calling struct destructors.
|
November 06, 2013 Re: How to re-initialise an associative array. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Maxim Fomin | On Wed, Nov 06, 2013 at 06:59:53PM +0100, Maxim Fomin wrote: > On Wednesday, 6 November 2013 at 17:49:41 UTC, H. S. Teoh wrote: > > > >The GC will take care of cleaning up the old data. > > > > > >T > > Unfortunately it will not take care of calling struct destructors. That applies regardless of how you re-initialize the AA, so that's nothing to do with this particular problem. Struct dtors are known to have many issues anyway. T -- People who are more than casually interested in computers should have at least some idea of what the underlying hardware is like. Otherwise the programs they write will be pretty weird. -- D. Knuth |
Copyright © 1999-2021 by the D Language Foundation