digitalmars.D.learn - How to re-initialise an associative array.
- Gary Willoughby (12/12) Nov 06 2013 A simple request but i'm failing hard. How do i re-init an
- JR (3/15) Nov 06 2013 Maybe there's a neater way but x = x.init works.
- Daniel Davidson (3/15) Nov 06 2013 x.clear();
- Gary Willoughby (6/27) Nov 06 2013 I looked at that but apparently it leaves the array in an unsafe
- Daniel Davidson (3/8) Nov 06 2013 Wow! Good to know, thanks!
- Daniel Davidson (21/25) Nov 06 2013 Is that still the case? The following seems to work just fine.
- Gary Willoughby (4/31) Nov 06 2013 I'm not taking the chance and currently using:
- Marco Leise (5/8) Nov 07 2013 x = null; is shorter. Just saying ;)
- Gary Willoughby (3/9) Nov 08 2013 Does that actually work? I thought you had to assign at least
- Dicebot (3/5) Nov 08 2013 For D arrays and AA's .init, null and [] are pretty much the same
- H. S. Teoh (21/23) Nov 06 2013 [...]
- Maxim Fomin (2/4) Nov 06 2013 Unfortunately it will not take care of calling struct destructors.
- H. S. Teoh (7/15) Nov 06 2013 That applies regardless of how you re-initialize the AA, so that's
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
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
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
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:I looked at that but apparently it leaves the array in an unsafe state. Source: http://forum.dlang.org/thread/iu3ll6$2d48$1 digitalmars.comA 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
On Wednesday, 6 November 2013 at 16:41:19 UTC, Gary Willoughby wrote:Wow! Good to know, thanks!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
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.comIs 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
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'm not taking the chance and currently using: x = (int[string]).init;I looked at that but apparently it leaves the array in an unsafe state. Source: http://forum.dlang.org/thread/iu3ll6$2d48$1 digitalmars.comIs 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
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
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>:Does that actually work? I thought you had to assign at least some value. I'm gonna test this.I'm not taking the chance and currently using: x = (int[string]).init;x = null; is shorter. Just saying ;)
Nov 08 2013
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
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
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. TUnfortunately it will not take care of calling struct destructors.
Nov 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: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. KnuthThe GC will take care of cleaning up the old data. TUnfortunately it will not take care of calling struct destructors.
Nov 06 2013