Jump to page: 1 2 3
Thread overview
Two-dimensional associateive arrays?
Feb 02, 2006
Tyler
Feb 02, 2006
Chris Sauls
Feb 02, 2006
Ivan Senji
Feb 02, 2006
Chris Sauls
Feb 02, 2006
Ivan Senji
Feb 02, 2006
Chris Sauls
Feb 03, 2006
Ivan Senji
Dynamic arrays with static elements (was: Re: Two-dimensional associateive arrays?)
Feb 09, 2006
Bruno Medeiros
Re: Dynamic arrays with static elements
Feb 09, 2006
Ivan Senji
Feb 10, 2006
Bruno Medeiros
Feb 06, 2006
Russ Lewis
Feb 07, 2006
Ivan Senji
Feb 07, 2006
Russ Lewis
Feb 08, 2006
Derek Parnell
Feb 08, 2006
Ivan Senji
Feb 02, 2006
Regan Heath
Feb 02, 2006
BCS
Feb 02, 2006
Regan Heath
Re: Two-dimensional associateive arrays?--Thanks everyone & a new question
Feb 03, 2006
Tyler
Feb 03, 2006
Regan Heath
Feb 03, 2006
Tyler
Feb 03, 2006
Tyler
Feb 04, 2006
James Dunne
Re: Two-dimensional associative arrays?--Thanks everyone & a new question
Feb 04, 2006
Deewiant
Re: Two-dimensional associative arrays?--Thanks everyone & a new
Feb 05, 2006
Tyler
February 02, 2006
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
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
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
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
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
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
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
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
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
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
« First   ‹ Prev
1 2 3