February 03, 2006 Re: Two-dimensional associateive arrays?--Thanks everyone & a new question | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tyler | Thanks for all your help! I decided that it would just be easier to use a struct like I did in the C version. Now I have another question though. I have a structure (not related to the first question) containing a 2 dynamic arrays of ints, like this: struct EXAMPLE_STRUCT { int[] fooLenghts; int[] barLenghts; } When I try to use either fooLenghts or barLenghts (see below for an example) DMD tells me that there is no property 'fooLenghts' for type 'EXAMPLE_STRUCT'. What am I doing wrong? void someFunc(inout EXAMPLE_STRUCT example) { example.fooLengths.length = length + 1; //DMD: no property 'fooLenghts' for type 'EXAMPLE_STRUCT' example.fooLenghts[length-1] = 55; //(same message) } |
February 03, 2006 Re: Two-dimensional associateive arrays?--Thanks everyone & a new question | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tyler | On Fri, 3 Feb 2006 04:37:04 +0000 (UTC), Tyler <Tyler_member@pathlink.com> wrote:
> Thanks for all your help! I decided that it would just be easier to use a
> struct like I did in the C version. Now I have another question though. I have
> a structure (not related to the first question) containing a 2 dynamic arrays of
> ints, like this:
>
> struct EXAMPLE_STRUCT {
> int[] fooLenghts;
> int[] barLenghts;
> }
>
> When I try to use either fooLenghts or barLenghts (see below for an example) DMD
> tells me that there is no property 'fooLenghts' for type 'EXAMPLE_STRUCT'. What
> am I doing wrong?
>
> void someFunc(inout EXAMPLE_STRUCT example)
> {
> example.fooLengths.length = length + 1; //DMD: no property 'fooLenghts' for type
> 'EXAMPLE_STRUCT'
> example.fooLenghts[length-1] = 55; //(same message)
> }
Typo "fooLengths" != "fooLenghts" the h and t are switched.
Regan
|
February 03, 2006 Re: Two-dimensional associateive arrays?--Thanks everyone & a new question | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | D'oh! Thanks for pointing out that typo. After staring at that code for a good while I have no clue how I missed it (and now it works!) |
February 03, 2006 Re: Two-dimensional associateive arrays?--Thanks everyone & a new question | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tyler | Alright, now I have another question. I finally got the program to compile, but when I run it I get an array out of bounds error while trying to do the following: struct INNER_STRUCT { int foo; int bar; } struct EAMPLE_STRUCT { OTHER_STRUCT[] unimportant; //(should be) unrelated to problem INNER_STRUCT[char[]] inner; } //global variable EXAMPLE_STRUCT example; /* ...SNIP... */ example.inner[exampString].foo = 15; //Generates ArrayBoundsError (exampString is a char[] of non-zero length) |
February 04, 2006 Re: Two-dimensional associateive arrays?--Thanks everyone & a new question | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tyler | Tyler wrote:
> Alright, now I have another question. I finally got the program to compile, but
> when I run it I get an array out of bounds error while trying to do the
> following:
>
> struct INNER_STRUCT {
> int foo;
> int bar;
> }
>
> struct EAMPLE_STRUCT {
> OTHER_STRUCT[] unimportant; //(should be) unrelated to problem
> INNER_STRUCT[char[]] inner;
> }
>
> //global variable
> EXAMPLE_STRUCT example;
>
> /* ...SNIP... */
>
> example.inner[exampString].foo = 15; //Generates ArrayBoundsError (exampString
> is a char[] of non-zero length)
>
>
I'd have to see more code...
|
February 04, 2006 Re: Two-dimensional associateive arrays?--Thanks everyone & a new question | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tyler | Looks to me like example.inner is a dynamic array. You'll probably have to do:
INNER_STRUCT temp_inner;
temp_inner.foo = 15;
example.inner[exampString] = temp_inner;
D used to create keys on read and write, but now it does so only on write. In your example, you're reading from the associative array, and then writing to the result of that.
For an example using code, consider this struct:
struct X
{
int y;
}
This code fails with an ArrayBoundsError:
int main()
{
X[char[]] x;
x["test"].y= 1;
return 0;
}
This code works:
int main()
{
X[char[]] x;
X temp_x;
temp_x.y = 1;
x["test"] = temp_x;
return 0;
}
You could also do this:
int main()
{
X[char[]] x;
X temp_x;
x["test"] = temp_x;
x["test"].y = 1;
return 0;
}
Or use a more complicated template.
Hope that helps,
-[Unknown]
> Alright, now I have another question. I finally got the program to compile, but
> when I run it I get an array out of bounds error while trying to do the
> following:
>
> struct INNER_STRUCT {
> int foo;
> int bar;
> }
>
> struct EAMPLE_STRUCT {
> OTHER_STRUCT[] unimportant; //(should be) unrelated to problem
> INNER_STRUCT[char[]] inner;
> }
>
> //global variable
> EXAMPLE_STRUCT example;
>
> /* ...SNIP... */
>
> example.inner[exampString].foo = 15; //Generates ArrayBoundsError (exampString
> is a char[] of non-zero length)
>
>
|
February 04, 2006 Re: Two-dimensional associative arrays?--Thanks everyone & a new question | ||||
---|---|---|---|---|
| ||||
Posted in reply to Unknown W. Brackets | Unknown W. Brackets wrote:
> This code works:
>
> int main()
> {
> X[char[]] x;
> X temp_x;
>
> temp_x.y = 1;
> x["test"] = temp_x;
>
> return 0;
> }
>
> You could also do this:
>
> int main()
> {
> X[char[]] x;
> X temp_x;
>
> x["test"] = temp_x;
> x["test"].y = 1;
>
> return 0;
> }
>
> Or use a more complicated template.
>
This also works, but I'm not so sure it's supposed to:
struct X { int y; }
void main() {
X[char[]] x;
x["test"] = *new X;
x["test"].y = 1;
}
Is it allowed to use new like that?
|
February 04, 2006 Re: Two-dimensional associative arrays?--Thanks everyone & a new question | ||||
---|---|---|---|---|
| ||||
Posted in reply to Deewiant | "Deewiant" <deewiant.doesnotlike.spam@gmail.com> wrote in message news:ds2q7t$2cgg$1@digitaldaemon.com... > This also works, but I'm not so sure it's supposed to: > > struct X { int y; } > > void main() { > X[char[]] x; > x["test"] = *new X; > x["test"].y = 1; > } > > Is it allowed to use new like that? Yeah, all you're doing is copying the values of the new X (i.e. the default values for an X) into the slot. You can also skip the new and just use x["test"] = X.init; That works as well. |
February 05, 2006 Re: Two-dimensional associative arrays?--Thanks everyone & a new question | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | Ah, that looks nicer. I should have done/thought of that.
-[Unknown]
> "Deewiant" <deewiant.doesnotlike.spam@gmail.com> wrote in message news:ds2q7t$2cgg$1@digitaldaemon.com...
>> This also works, but I'm not so sure it's supposed to:
>>
>> struct X { int y; }
>>
>> void main() {
>> X[char[]] x;
>> x["test"] = *new X;
>> x["test"].y = 1;
>> }
>>
>> Is it allowed to use new like that?
>
> Yeah, all you're doing is copying the values of the new X (i.e. the default values for an X) into the slot. You can also skip the new and just use
>
> x["test"] = X.init;
>
> That works as well.
>
>
|
February 05, 2006 Re: Two-dimensional associative arrays?--Thanks everyone & a new | ||||
---|---|---|---|---|
| ||||
Posted in reply to Unknown W. Brackets | Thanks again everyone. |
Copyright © 1999-2021 by the D Language Foundation