associative array of associative arrays.

dcoder dcoder at devnull.dev
Fri Aug 13 07:10:43 PDT 2010


== Quote from dennis luehring (dl.soluz at gmx.net)'s article
> 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;

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



More information about the Digitalmars-d-learn mailing list