Thread overview | ||||||
---|---|---|---|---|---|---|
|
May 30, 2015 Appending to multidimensional dynamic array | ||||
---|---|---|---|---|
| ||||
I want to append a 2D array to my 3D array. I expect it should be same as int[] arr; arr ~= 3; void readInput() { char[][][] candidate; char[] buff; size_t counter = 0; while ( stdin.readln(buff) ) { char[][] line = buff.chomp().split(); writeln(line); candidate ~= line; writeln(candidate); if (++counter > 1 ) break; } } And I send the inputs below 201212?4 64 20121235 93 I expect a output like [["201212?4", "64"], ["20121235", "93"]] But instead I see [["20121235", "93"], ["20121235", "93"]] In short : =~ replaces all the elements in the array with the last added. Where am I doing wrong? How can I meet my expectation? By the way I am posting the same question to stackoverflow at the same time. Does sending questions to stackoverflow as well as here not desirable for D community? If so I will just write here. |
May 30, 2015 Re: Appending to multidimensional dynamic array | ||||
---|---|---|---|---|
| ||||
Posted in reply to kerdemdemir | On 31/05/2015 12:41 a.m., kerdemdemir wrote:
> I want to append a 2D array to my 3D array. I expect it should be same
> as int[] arr; arr ~= 3;
>
> void readInput()
> {
> char[][][] candidate;
> char[] buff;
> size_t counter = 0;
> while ( stdin.readln(buff) )
> {
> char[][] line = buff.chomp().split();
> writeln(line);
>
> candidate ~= line;
> writeln(candidate);
> if (++counter > 1 ) break;
> }
> }
>
> And I send the inputs below
>
> 201212?4 64
> 20121235 93
> I expect a output like
>
> [["201212?4", "64"], ["20121235", "93"]]
> But instead I see
>
> [["20121235", "93"], ["20121235", "93"]]
>
> In short :
> =~ replaces all the elements in the array with the last added. Where am
> I doing wrong? How can I meet my expectation?
>
>
> By the way I am posting the same question to stackoverflow at the same
> time. Does sending questions to stackoverflow as well as here not
> desirable for D community? If so I will just write here.
stackoverflow isn't normally looked at by those in the D community.
In your case its pretty simple. The buffer is being reused. So same memory being added multiple times to the candidate.
Just slap on a .dup when adding it.
It'll duplicate the contents in new memory so it won't be assigned on top of.
|
May 30, 2015 Re: Appending to multidimensional dynamic array | ||||
---|---|---|---|---|
| ||||
Posted in reply to kerdemdemir | On Saturday, 30 May 2015 at 12:41:42 UTC, kerdemdemir wrote:
> By the way I am posting the same question to stackoverflow at the same time. Does sending questions to stackoverflow as well as here not desirable for D community? If so I will just write here.
I like it there, SO is a bit easier to search for the wider userbase and I tend to answer there quickly if I'm online at the time.
|
May 31, 2015 Re: Appending to multidimensional dynamic array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rikki Cattermole | > In your case its pretty simple. The buffer is being reused. So same memory being added multiple times to the candidate. > Just slap on a .dup when adding it. >> candidate ~= line; Thanks a lot replacing the line above with candidate ~= [line[0].dup, line[1].dup]; My problem is solved . |
Copyright © 1999-2021 by the D Language Foundation