digitalmars.D - Initialization of associative arrays
- alxdef (3/3) Jun 23 2006 How to initialize associative array like this:
- Andrei Khropov (6/11) Jun 23 2006 I don't think you can initialize them this way.
-
Derek Parnell
(37/39)
Jun 23 2006
On Fri, 23 Jun 2006 23:19:53 +1000, alxdef
...
How to initialize associative array like this: const some_type [char[]] arr = ... ? Thanks a lot!
Jun 23 2006
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
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