www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - associative array definition

reply llee <llee goucher.edu> writes:
Is it possible to define associative arrays using a format similar to that used
to define other types of arrays? For example I can define an integer array
using: 
     int[] array = [1, 2, 3];
I can define an associative array using:
     string [string] array;
     array ["key"] = "value;
But I would like to use a static declaration like the one above. 
Jul 11 2008
parent bearophile <bearophileHUGS lycos.com> writes:
llee:
 But I would like to use a static declaration like the one above. 

I suggest you to use the d.learn group for this kind of questions. I think you can't define AAs statically, but you can do something like: string[char] map; static this() { map = ['a':"foo"[], 'b':"bar"]; } the static this() is executed before the main(). ---------------- Note for the other people, static AAs (and static sets, if D will ever have them) can enjoy perfect hashing, this is a recent note of mine: http://groups.google.com/group/comp.lang.python/browse_thread/thread/09363c0d50a804bb# Bye, bearophile
Jul 11 2008