use variant as associative array
GG
g at g.com
Thu Feb 11 22:53:23 PST 2010
I tried std.boxer,
Box[char[]][int] bb;
bb[0]["Month"] = box("Jan");
bb[0]["Profit"] = box(800);
bb[1]["Month"] = box("Jan");
bb[1]["Profit"] = box(200);
/*section work*/
writefln("%s",bb[0]["Month"]);
writefln("%d",bb[0]["Profit"]);
writefln("%s",bb[1]["Month"]);
writefln("%d",bb[1]["Profit"]);
/*end section*/
for(int i=0;i<bb.length;i++) // can't do .length with box, so can't pass on array :-(
{
writefln("%s",bb[i]["Month"]);
writefln("%d",bb[i]["Profit"]);
}
But I found this : http://www.digitalmars.com/d/archives/D/gnu/Issue_1968_New_boxer.d_does_not_work_3168.html
They say to use std.variant for new code...
So with std.boxer, we can save and get data by indice directly but no property .length
With std.variant, we can save data but can't access to data (ex: aa[i]["Month"]) // Range violation
Thanks !
More information about the Digitalmars-d-learn
mailing list