Making associatvie array from array of pairs

Timon Gehr timon.gehr at gmx.ch
Tue Dec 24 06:39:16 PST 2013


On 12/24/2013 12:36 PM, Dfr 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", ...];
>
>

void main(){
     import std.array, std.algorithm, std.typecons;
     auto a = [["1","0000FF"], ["2", "00FF00"], /+...+/];
     auto aa = a.map!(x=>tuple(x[0],x[1])).assocArray;

     import std.stdio;
     writeln(aa);
}



More information about the Digitalmars-d-learn mailing list