www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to re-initialise an associative array.

reply "Gary Willoughby" <dev nomad.so> writes:
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.
	}
Nov 06 2013
next sibling parent "JR" <zorael gmail.com> writes:
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.
Nov 06 2013
prev sibling next sibling parent reply "Daniel Davidson" <nospam spam.com> writes:
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();
Nov 06 2013
parent reply "Gary Willoughby" <dev nomad.so> writes:
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
Nov 06 2013
next sibling parent "Daniel Davidson" <nospam spam.com> writes:
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!
Nov 06 2013
prev sibling parent reply "Daniel Davidson" <nospam spam.com> writes:
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
Nov 06 2013
parent reply "Gary Willoughby" <dev nomad.so> writes:
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;
Nov 06 2013
parent reply Marco Leise <Marco.Leise gmx.de> writes:
Am Wed, 06 Nov 2013 18:14:54 +0100
schrieb "Gary Willoughby" <dev nomad.so>:

 I'm not taking the chance and currently using:
 
 x = (int[string]).init;
x = null; is shorter. Just saying ;) -- Marco
Nov 07 2013
parent reply "Gary Willoughby" <dev nomad.so> writes:
On Thursday, 7 November 2013 at 15:50:05 UTC, Marco Leise wrote:
 Am Wed, 06 Nov 2013 18:14:54 +0100
 schrieb "Gary Willoughby" <dev nomad.so>:

 I'm not taking the chance and currently using:
 
 x = (int[string]).init;
x = null; is shorter. Just saying ;)
Does that actually work? I thought you had to assign at least some value. I'm gonna test this.
Nov 08 2013
parent "Dicebot" <public dicebot.lv> writes:
On Friday, 8 November 2013 at 09:04:28 UTC, Gary Willoughby wrote:
 Does that actually work? I thought you had to assign at least 
 some value. I'm gonna test this.
For D arrays and AA's .init, null and [] are pretty much the same thing.
Nov 08 2013
prev sibling parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
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.
Nov 06 2013
parent reply "Maxim Fomin" <maxim maxim-fomin.ru> writes:
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.
Nov 06 2013
parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
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
Nov 06 2013