Thread overview |
---|
August 24, 2019 Merging two associative arrays | ||||
---|---|---|---|---|
| ||||
I've got two associative arrays and want to get a new one, which is created out of both of them: This works: string[int] a = [1:"one", 7:"seven"]; string[int] b = [5:"five", 9:"nine"]; string[int] tmp = a.dup; foreach (k,v;b) tmp[k] = v; assert(tmp==[1:"one", 7:"seven", 5:"five", 9:"nine"]); But is there something easier, especially without making that "tmp" explicit. I hoped for a~b, but that didn't work. (I allready know, that there aren't duplicated keys, if that matters.) |
August 24, 2019 Re: Merging two associative arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to berni | On Saturday, 24 August 2019 at 19:35:25 UTC, berni wrote:
> I've got two associative arrays and want to get a new one, which is created out of both of them:
>
> This works:
>
> string[int] a = [1:"one", 7:"seven"];
> string[int] b = [5:"five", 9:"nine"];
>
> string[int] tmp = a.dup;
> foreach (k,v;b) tmp[k] = v;
>
> assert(tmp==[1:"one", 7:"seven", 5:"five", 9:"nine"]);
>
> But is there something easier, especially without making that "tmp" explicit. I hoped for a~b, but that didn't work. (I allready know, that there aren't duplicated keys, if that matters.)
auto ab = a.byPair.chain( b.byPair).assocArray ?
|
August 25, 2019 Re: Merging two associative arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to a11e99z | On Saturday, 24 August 2019 at 19:55:48 UTC, a11e99z wrote:
> auto ab = a.byPair.chain( b.byPair).assocArray ?
Not sure, if it is simpler, but a least without tmp. :) Thanks.
|
Copyright © 1999-2021 by the D Language Foundation