On 08/13/2010 01:17 AM, dcoder wrote:
Hello. How do you declare and initialize a map that looks like the following:
Name => [ Personal Info]
Where personal info is type string[string].
I can't get this to compile. I'm wondering what I am doing wrong:
import std.stdio;
void main() {
int[string] helloworld = [ "Hello":0, "World":1 ];
foreach( k,v;helloworld) {
writefln( "%s -> %s", k, v);
}
writefln( "helloworld type: %s", typeid(helloworld));
string[string] table = [ "City":"Boston", "Title":"Vice President" ];
foreach( k, v; table) {
writefln( "%s: %s", k, v);
}
// Here's the problem:
string[string[string]] leaders = [ "Obama":["City":"DC", "Title":"ThePrez"],
"Cameron":["City":"London",
"Title":"DaPrimeMinista"]];
foreach( k, v; leaders) {
writefln( "first foreach type: %s", typeid(v));
writefln( "Person: %s", k);
foreach( kk, vv; v) {
writefln( "\t%s\t%s", kk, vv);
}
}
return;
}
Here's the output:
$ dmd AssocArray.d
AssocArray.d(25): Error: Integer constant expression expected instead of "City"
AssocArray.d(25): Error: Integer constant expression expected instead of "Title"
AssocArray.d(25): Error: Integer constant expression expected instead of "City"
AssocArray.d(25): Error: Integer constant expression expected instead of "Title"
AssocArray.d(24): Error: not an associative array initializer
$ dmd --help
Digital Mars D Compiler v2.047
Copyright (c) 1999-2010 by Digital Mars written by Walter Bright
Documentation: http://www.digitalmars.com/d/2.0/index.html
thanks
dcoder
This is probably a bug, I think I have run into it.
Try this:
string[string[string]] leaders;
leaders["Obama"] = ["City":"DC", "Title":"ThePrez"];
leaders["Cameron"] = ["City":"London", "Title":"DaPrimeMinista"];
And if that doesn't work, try unfolding more :-)
Probably should report this as a bug, as well.