Thread overview |
---|
February 02, 2013 Associative Array, key and value as type of string or char[] | ||||
---|---|---|---|---|
| ||||
Hi all, in the moment I'm running against a wall: I want to put some strings together into an ass. array: For example a command with it's description like the following. char[char[]] ch_Description = [ 'kill' : 'kills a process', 'pause' : 'pauses a process' ] I tried with all combinations of char[char[]] over string[char[]], char[string] to string[string] with depending quotations 'abc' to "abc". My idea is just to print out the combinations to console. I'm not even .sure, if it's a good idea to do so, but what makes me cry is, after all trying, is that I can't find a solution. The compiler messages go from "no implicit conversion" to "cast(char) of string is deprecated" and others. I read much about that key-values have to be immutable and so on and I can accept the reasons, but I can't believe, there's no possible solution for my underlaying idea. Any ideas? timewulf |
February 02, 2013 Re: Associative Array, key and value as type of string or char[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to timewulf | It seems you are right. I get the same error. You should open a bug report for this. But as long as it isn't fixed you can use an output via foreach. 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); } } |
February 02, 2013 Re: Associative Array, key and value as type of string or char[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namespace | 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!
|
February 02, 2013 Re: Associative Array, key and value as type of string or char[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namespace | On 2013-02-02 16:06, Namespace wrote:
> string[string] ch_Description = [
> "kill" : "kills a process",
> "pause" : "pauses a process"
> ];
>
> writeln(ch_Description); // does not work, but it should
Doesn't work in the current version? In DMD 2.060 it works.
|
February 02, 2013 Re: Associative Array, key and value as type of string or char[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to FG | On Saturday, 2 February 2013 at 16:06:09 UTC, FG wrote:
> On 2013-02-02 16:06, Namespace wrote:
>> string[string] ch_Description = [
>> "kill" : "kills a process",
>> "pause" : "pauses a process"
>> ];
>>
>> writeln(ch_Description); // does not work, but it should
>
> Doesn't work in the current version? In DMD 2.060 it works.
I've cloned the github version two days ago and with that I get lots of errors.
Maybe I should open a bug report. :)
|
February 02, 2013 Re: Associative Array, key and value as type of string or char[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to FG | On 02.02.2013 17:06, FG wrote:
> On 2013-02-02 16:06, Namespace wrote:
>> string[string] ch_Description = [
>> "kill" : "kills a process",
>> "pause" : "pauses a process"
>> ];
>>
>> writeln(ch_Description); // does not work, but it should
>
> Doesn't work in the current version? In DMD 2.060 it works.
I tried in dmd 2.061 and git-version from yesterday: Both work!
|
February 02, 2013 Re: Associative Array, key and value as type of string or char[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namespace | Works now. |
February 03, 2013 Re: Associative Array, key and value as type of string or char[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to timewulf | On 02/02/2013 08:00 AM, timewulf wrote: > After searching for the differences to my code, I found my fault: It is not your fault. :) > Now I've just to look, how to declare this array in the way of public > constants. You need 'static this()': import std.stdio; string[string] ch_Description; static this() { ch_Description = [ "kill" : "kills a process", "pause" : "pauses a process" ]; } void main() { // ... } You can even make the AA immutable if it never changes: immutable string[string] ch_Description; Ali |
February 03, 2013 Re: Associative Array, key and value as type of string or char[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On 03.02.2013 06:09, Ali Çehreli wrote:
> You need 'static this()':
>
> import std.stdio;
>
> string[string] ch_Description;
>
> static this()
> {
> ch_Description = [ "kill" : "kills a process",
> "pause" : "pauses a process" ];
> }
>
> void main() {
> // ...
> }
>
> You can even make the AA immutable if it never changes:
>
> immutable string[string] ch_Description;
>
> Ali
>
Thanks, I applied both and errors are gone.
Timewulf
|
Copyright © 1999-2021 by the D Language Foundation