Thread overview | ||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
February 02, 2006 Two-dimensional associateive arrays? | ||||
---|---|---|---|---|
| ||||
I'm working on converting a program from C to D and one of the data structures in it could be more easily represented in D as an associative array of arrays of 2 integers (i.e. "int[char[]][2] example;") Can D do this? The latgest DMD compiler lets me declare it, but when I try to use it ("example["foo"][1] = 55;") it gives me errors about how it can't implicitly cast char[] to int. |
February 02, 2006 Re: Two-dimensional associateive arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tyler | Tyler wrote:
> I'm working on converting a program from C to D and one of the data structures
> in it could be more easily represented in D as an associative array of arrays of
> 2 integers (i.e. "int[char[]][2] example;") Can D do this? The latgest DMD
> compiler lets me declare it, but when I try to use it ("example["foo"][1] =
> 55;") it gives me errors about how it can't implicitly cast char[] to int.
>
>
Its a simple, and common mistake for thsoe new to D arrays. The basic thing to remember, is that decleration and usage are in reverse syntactic directions, and declerations are "read" from right to left. In other words:
# // declare
# int[2][char[]] example;
#
# // use
# example["foo"c][1] = 55;
In the case above, the decleration "reads" as "an association of char[]'s to static arrays, length 2, of int's." Your original decleration "reads" as "a static array, length 2, of associations of char[]'s to int's."
-- Chris Sauls
|
February 02, 2006 Re: Two-dimensional associateive arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tyler | On Thu, 2 Feb 2006 20:23:00 +0000 (UTC), Tyler <Tyler_member@pathlink.com> wrote:
> I'm working on converting a program from C to D and one of the data structures
> in it could be more easily represented in D as an associative array of arrays of
> 2 integers (i.e. "int[char[]][2] example;") Can D do this? The latgest DMD
> compiler lets me declare it, but when I try to use it ("example["foo"][1] =
> 55;") it gives me errors about how it can't implicitly cast char[] to int.
I believe that sort of array should be declared:
int[2][char[]] example;
Further "55;" is a string literal, in this case a char[], not a fixed array of 2 integers.
However, part of the problem you are going to have is the inability to define a fixed array of 2 integers without making it "static".
void foo(int[2] a) {}
void main()
{
int[2] a = [1,2]; //fails
static int[2] b = [1,2]; //works
foo(b); //works
foo([2,3]); //fails
}
Further, you can't create a dynamic array of integers either, eg.
void bar(int[] a) {}
void main()
{
bar([2,3]); //fails
}
I believe the intention is to support this at a later stage.
However, right now there are a number of templates which can achieve this. I don't have one but someone else is likely to post one for you. So, I suspect the way you're going to have to solve this is..
int[][char[]] example;
example["label"] = makeArray!(int)(1,2);
or similar, where makeArray is the template I mentioned. Alternately if your values come in string form a function like..
int[] parseIntPair(char[] data)
{
}
which converts from the string into a dynamic array of integers could be used.
Regan
|
February 02, 2006 Re: Two-dimensional associateive arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris Sauls | Chris Sauls wrote:
> Tyler wrote:
>
>> I'm working on converting a program from C to D and one of the data structures
>> in it could be more easily represented in D as an associative array of arrays of
>> 2 integers (i.e. "int[char[]][2] example;") Can D do this? The latgest DMD
>> compiler lets me declare it, but when I try to use it ("example["foo"][1] =
>> 55;") it gives me errors about how it can't implicitly cast char[] to int.
>>
>>
>
> Its a simple, and common mistake for thsoe new to D arrays. The basic thing to remember, is that decleration and usage are in reverse syntactic directions, and declerations are "read" from right to left. In other words:
>
> # // declare
> # int[2][char[]] example;
> #
> # // use
> # example["foo"c][1] = 55;
Common mistake indeed, but the above doesn't work. Instead you get
ArrayBoundsError. Why would that be?
|
February 02, 2006 Re: Two-dimensional associateive arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ivan Senji | Ivan Senji wrote:
> Chris Sauls wrote:
>
>> Tyler wrote:
>>
>>> I'm working on converting a program from C to D and one of the data structures
>>> in it could be more easily represented in D as an associative array of arrays of
>>> 2 integers (i.e. "int[char[]][2] example;") Can D do this? The latgest DMD
>>> compiler lets me declare it, but when I try to use it ("example["foo"][1] =
>>> 55;") it gives me errors about how it can't implicitly cast char[] to int.
>>>
>>>
>>
>> Its a simple, and common mistake for thsoe new to D arrays. The basic thing to remember, is that decleration and usage are in reverse syntactic directions, and declerations are "read" from right to left. In other words:
>>
>> # // declare
>> # int[2][char[]] example;
>> #
>> # // use
>> # example["foo"c][1] = 55;
>
>
> Common mistake indeed, but the above doesn't work. Instead you get
> ArrayBoundsError. Why would that be?
I had to think about it for a bit, but I know why... and it dregs up an ancient topic. It issues the ArrayBoundsError because the key "foo" does not yet exist. Now, normally an assignment expression creates the key, but apparently when one includes extra dimensions then D doesn't catch this.
Bug? Or annoying feature?
-- Chris Sauls
|
February 02, 2006 Re: Two-dimensional associateive arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | Regan Heath wrote: > On Thu, 2 Feb 2006 20:23:00 +0000 (UTC), Tyler <Tyler_member@pathlink.com> wrote: with a little reformatting... >> [...] ("example["foo"][1] = 55;") [...] quoted code, not quotes in code. > > Further "55;" is a string literal, in this case a char[], not a fixed > |
February 02, 2006 Re: Two-dimensional associateive arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris Sauls | Chris Sauls wrote: > I had to think about it for a bit, but I know why... and it dregs up an ancient topic. It issues the ArrayBoundsError because the key "foo" does not yet exist. Now, normally an assignment expression creates the key, but apparently when one includes extra dimensions then D doesn't catch this. > > Bug? Or annoying feature? As you said assignment normally creates the key, so i would vote for bug. But the bigger problem is how would one manully create a static array. It just isn't possible. If examples was int[][char[]] example, you could do something like example["foo"] = makeArray!(int)(55,56); but in this case is there anything that can be done? > > -- Chris Sauls |
February 02, 2006 Re: Two-dimensional associateive arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | On Thu, 02 Feb 2006 12:52:32 -0800, BCS <BCS_member@pathlink.com> wrote:
> Regan Heath wrote:
>> On Thu, 2 Feb 2006 20:23:00 +0000 (UTC), Tyler <Tyler_member@pathlink.com> wrote:
>
> with a little reformatting...
>
> >> [...] ("example["foo"][1] = 55;") [...]
>
> quoted code, not quotes in code.
>
>> Further "55;" is a string literal, in this case a char[], not a fixed
Oops, my apologies.
Regan
|
February 02, 2006 Re: Two-dimensional associateive arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ivan Senji | Ivan Senji wrote: > Chris Sauls wrote: > >> I had to think about it for a bit, but I know why... and it dregs up an ancient topic. It issues the ArrayBoundsError because the key "foo" does not yet exist. Now, normally an assignment expression creates the key, but apparently when one includes extra dimensions then D doesn't catch this. >> >> Bug? Or annoying feature? > > > As you said assignment normally creates the key, so i would vote for bug. > > But the bigger problem is how would one manully create a static array. It just isn't possible. > > If examples was int[][char[]] example, you could do something like > example["foo"] = makeArray!(int)(55,56); but in this case is there anything that can be done? > >> >> -- Chris Sauls # template makeStaticArray!(T_Type, size_t T_Size) { # T_Type[T_Size] makeStaticArray(T_Type[] data ...) { # if (data.length == T_Size) # return cast(T_Type[T_Size]) data; # throw new Exception(format("makeStaticArray: received %d items, expected %d"), data.length, T_Size)); # } # } Maybe? -- Chris Sauls |
February 03, 2006 Re: Two-dimensional associateive arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris Sauls | Chris Sauls wrote: > Ivan Senji wrote: >> >> As you said assignment normally creates the key, so i would vote for bug. >> >> But the bigger problem is how would one manully create a static array. It just isn't possible. >> >> If examples was int[][char[]] example, you could do something like >> example["foo"] = makeArray!(int)(55,56); but in this case is there anything that can be done? >> >>> >>> -- Chris Sauls > > > # template makeStaticArray!(T_Type, size_t T_Size) { > # T_Type[T_Size] makeStaticArray(T_Type[] data ...) { > # if (data.length == T_Size) > # return cast(T_Type[T_Size]) data; > # throw new Exception(format("makeStaticArray: received %d items, expected %d"), data.length, T_Size)); > # } > # } > Nice template.. > Maybe? But with the folowing problems: - functions can not return static arrays - you can not assign to static array |
Copyright © 1999-2021 by the D Language Foundation