Best way to duplicate an associative array

Jarrett Billingsley kb3ctd2 at yahoo.com
Thu Mar 23 06:29:43 PST 2006


"Paolo Invernizzi" <arathorn at NOSPAM_fastwebnet.it> wrote in message 
news:dvu0oj$qbo$1 at digitaldaemon.com...
> Hi all,
>
> As the subject, there's a way to easy to .dup and associative array?
>
> Thanks

template dup(T)
{
 T dup(T aa)
 {
  auto keys = aa.keys;

  T aa2;

  foreach(key; keys)
   aa2[key] = aa[key];

  return aa2;
 }
}

void main()
{
 int[char[]] aa;
 aa["hello"] = 5;
 aa["fork"] = 10;

 int[char[]] aa2 = aa.dup();

 foreach(char[] key, int value; aa)
  writefln(key, ": ", value);

 writefln();

 foreach(char[] key, int value; aa2)
  writefln(key, ": ", value);
}

Try that on for size :) 





More information about the Digitalmars-d mailing list