Jump to page: 1 2
Thread overview
How to re-initialise an associative array.
Nov 06, 2013
Gary Willoughby
Nov 06, 2013
JR
Nov 06, 2013
Daniel Davidson
Nov 06, 2013
Gary Willoughby
Nov 06, 2013
Daniel Davidson
Nov 06, 2013
Daniel Davidson
Nov 06, 2013
Gary Willoughby
Nov 07, 2013
Marco Leise
Nov 08, 2013
Gary Willoughby
Nov 08, 2013
Dicebot
Nov 06, 2013
H. S. Teoh
Nov 06, 2013
Maxim Fomin
Nov 06, 2013
H. S. Teoh
November 06, 2013
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
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
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
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
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
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
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
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
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
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
« First   ‹ Prev
1 2