use variant as associative array
Daniel Keep
daniel.keep.lists at gmail.com
Thu Feb 11 18:13:51 PST 2010
Daniel Keep wrote:
> I got the following to work with Tango's Variant (dmd 1.051 + pre-0.99.9
> trunk):
>
> Variant[char[]][int] aa;
>
> aa[0] = typeof(aa[0]).init;
> aa[0]["Month"] = Variant("Jan");
> aa[0]["Profit"] = Variant(500);
>
> aa[1] = typeof(aa[0]).init;
> aa[1]["Month"] = Variant("Feb");
> aa[1]["Profit"] = Variant(800);
>
> There's actually two problems at work here:
>
> 1. You can't use a nested associative array without initialising it.
Scratch that; it does work. Huh. Fancy that.
> 2. You apparently can't assign to an aa of Variants for some reason.
>
> Keep in mind that Variants are basically a hack on the language; that
> assigning to a Variant value in an AA doesn't work isn't entirely
> surprising.
Another thing that occurred to me; is there any reason you can't do this:
> struct Record
> {
> char[] month;
> int profit;
> }
>
> Record[int] aa;
>
> aa[0] = Record("Jan", 500);
> aa[1] = Record("Feb", 800);
Or even this:
> Record[] arr;
> arr ~= Record("Jan", 500);
> arr ~= Record("Feb", 800);
Or even even this:
> enum Month { Jan, Feb, Mar, /* and the rest */ }
> struct Record { Month month; int profit }
> Record[] arr;
> arr ~= Record(Month.Jan, 500);
> arr ~= Record(Month.Feb, 800);
?
More information about the Digitalmars-d-learn
mailing list