digitalmars.D.learn - associative array of associative arrays.
- dcoder (40/40) Aug 12 2010 Hello. How do you declare and initialize a map that looks like the foll...
- dennis luehring (5/45) Aug 12 2010 Am 13.08.2010 01:17, schrieb dcoder:
- dcoder (14/90) Aug 13 2010 Ah, thanks.
- Pelle (8/48) Aug 13 2010 This is probably a bug, I think I have run into it.
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
Aug 12 2010
Am 13.08.2010 01:17, schrieb dcoder:
string[string][string] leaders
or try using alias to "see" the light
alias string[string] city_info;
city_info[string] leaders;
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
Aug 12 2010
== Quote from dennis luehring (dl.soluz gmx.net)'s articleAm 13.08.2010 01:17, schrieb dcoder: string[string][string] leaders or try using alias to "see" the light alias string[string] city_info; city_info[string] leaders;Ah, thanks. string[string[string]] maps type string[string] as key to string, which is the reverse of what I want. I want to map string to string[string]. So, anyways, I did make the change, but my initialization still doesn't work. I get the same errors: $ 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 Why? And why would I get the same kind of error? thanks.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
Aug 13 2010
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.
Aug 13 2010









dcoder <dcoder devnull.dev> 