Associative Array, key and value as type of string or char[]

timewulf timewulf at network-application.de
Sat Feb 2 08:00:21 PST 2013


On 02.02.2013 16:06, Namespace wrote:
> import std.stdio;
> 
> void main() {
>     string[string] ch_Description = [
>         "kill" : "kills a process",
>         "pause" : "pauses a process"
>     ];
>     
>     // writeln(ch_Description); // does not work, but it should
>     foreach (string key, string value; ch_Description) {
>         writeln(key, ':', value);
>     }
> }

Thanks for the answer.

I tried your code and very surprisingly: It works on both ways.

After searching for the differences to my code, I found my fault:

import std.stdio;

    string[string] ch_Description = [
        "kill" : "kills a process",
        "pause" : "pauses a process"
    ];

void main() {

    writeln(ch_Description); // does not work, but it should
    foreach (string key, string value; ch_Description) {
        writeln(key, ':', value);
    }
}

I declared the array outside of main(), because I wanted to use them in
more than one function. The errors I got were all, because of this first
one,

"src/tstsuite.d(3): Error: non-constant expression ["kill":"kills a
process","pause":"pauses a process"]"

which shows up when compiling this simple example.

After checking this, I stripped down my program from unneeded casts and
so on and now I see the same error message as above. It just didn't come
up, because of my workarounds, which created the other failures.

Thanks for showing me the forest behind this big tree ;-)

Now I've just to look, how to declare this array in the way of public
constants.

Thanks and have nice weekend!


More information about the Digitalmars-d-learn mailing list