www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Initialization of associative arrays

reply alxdef <alxdef_member pathlink.com> writes:
How to initialize associative array like this:

const some_type [char[]] arr = ... ?

Thanks a lot!
Jun 23 2006
next sibling parent "Andrei Khropov" <andkhropov nospam_mtu-net.ru> writes:
alxdef wrote:

 How to initialize associative array like this:
 
 const some_type [char[]] arr = ... ?
 
 Thanks a lot!
I don't think you can initialize them this way. See http://www.digitalmars.com/d/arrays.html#associative. P.S. You should better ask such questions in digitalMars.D.learn newsgroup. -- AKhropov
Jun 23 2006
prev sibling parent "Derek Parnell" <derek psych.ward> writes:
On Fri, 23 Jun 2006 23:19:53 +1000, alxdef <alxdef_member pathlink.com> =
 =

wrote:

 How to initialize associative array like this:

 const some_type [char[]] arr =3D ... ?
D does not yet support compile-time initialization of associative arrays= . = Such arrays need to be initialized at run time, most usually in the modu= le = constructor ... --- sample ----------- import std.stdio; const int red =3D 45; const int orange =3D 71; const int yellow =3D 13; const int blue =3D 92; const int green =3D 88; const int [char[]] arr; static this() { arr["apple"] =3D red; arr["orange"] =3D orange; arr["banana"] =3D yellow; arr["berry"] =3D blue; } void main() { foreach(char[] k, int c; arr) writefln("%s is %s", k, c); } -------------------- Note that you can still have a 'const' associative array even though it = is = initialized at run time because such an array can *only* be initialized = in = the module constructor. -- = Derek Parnell Melbourne, Australia
Jun 23 2006