Making associatvie array from array of pairs

Philippe Sigaud philippe.sigaud at gmail.com
Tue Dec 24 03:49:03 PST 2013


On Tue, Dec 24, 2013 at 12:36 PM, Dfr <deflexor at yandex.ru> wrote:
> Let's say i have array of kind:
>
> auto a = [["1","0000FF"], ["2", "00FF00"], ...];
>
> Is there simple way to turn it into associative array of kind:
>
> string[string] b = ["1": "0000FF", "2": "00FF00", ...];

To build a value (your b) from a range (a), you can use
std.algorithm.reduce, it's a very generic algo:


    auto a = [["1","0000FF"], ["2", "00FF00"]];
    import std.algorithm: reduce;
   string[string] b;
    b = reduce!((aa, pair) { aa[pair[0]] = pair[1]; return aa;})(b,a);
    writeln(b);


More information about the Digitalmars-d-learn mailing list