Danol
Danol

Reputation: 408

How to create an associative array from a range in D

Let's say I have an array of type Record[] and I want to create an associative array from it, key being rec.key. Is there an easy way to do it?

Upvotes: 3

Views: 148

Answers (1)

Danol
Danol

Reputation: 408

Yes, you can use std.array, std.typecons and std.algorithm libraries and construct this one-liner:

Record[Key] assocArray = array.map!( item => tuple( item.key, item ) ).assocArray;

It takes array, maps it to a tuple (Key, Record) and then takes that list of tuples and creates an associative array from it.

Upvotes: 6

Related Questions