associative array of associative arrays.

dcoder dcoder at devnull.dev
Thu Aug 12 16:17:55 PDT 2010


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