Thread overview
Initialization of associative arrays
Jun 23, 2006
alxdef
Jun 23, 2006
Andrei Khropov
Jun 23, 2006
Derek Parnell
June 23, 2006
How to initialize associative array like this:

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

Thanks a lot!


June 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
June 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 = ... ?
>

D does not yet support compile-time initialization of associative arrays. Such arrays need to be initialized at run time, most usually in the module constructor ...

--- sample -----------
import std.stdio;
const int red = 45;
const int orange = 71;
const int yellow = 13;
const int blue = 92;
const int green = 88;

const int [char[]] arr;

static this()
{
    arr["apple"] = red;
    arr["orange"] = orange;
    arr["banana"] = yellow;
    arr["berry"] = 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