Thread overview
Array references
Jul 22, 2004
Cabal
Jul 22, 2004
Cabal
Jul 22, 2004
Stewart Gordon
Jul 22, 2004
Cabal
Jul 22, 2004
Sean Kelly
July 22, 2004
I have a ragged array hashmap ('int[][char[]] myArray') on which I would *like* to be able to lookup the 'int[]' part and do operations on its attributes. Eg...

,----[  ]
| int[] myRow = myArray["test"];
| if (myRow.length <= idx) myRow.length = idx + 1;
| myRow[idx] = 10;
| ...
| // other code
| ...
| int myInt = myArray["test"][idx]      // Bang! ArrayBoundsError
`----

Unfortunately I can't seem to find any way to take a reference to the 'int[]' parts I want to manipulate. Operating on the slices produced by the above code leaves the original array attributes as they were.


July 22, 2004
For the pendants before you start :) I know that its not a hashmap, its a D associative array - it just didn't read right and the terminology doesn't really have a bearing on the issue at hand.

Cabal wrote:

> I have a ragged array hashmap ('int[][char[]] myArray') on which I would *like* to be able to lookup the 'int[]' part and do operations on its attributes. Eg...
> 
> ,----[  ]
> | int[] myRow = myArray["test"];
> | if (myRow.length <= idx) myRow.length = idx + 1;
> | myRow[idx] = 10;
> | ...
> | // other code
> | ...
> | int myInt = myArray["test"][idx]      // Bang! ArrayBoundsError
> `----
> 
> Unfortunately I can't seem to find any way to take a reference to the 'int[]' parts I want to manipulate. Operating on the slices produced by the above code leaves the original array attributes as they were.

July 22, 2004
Cabal wrote:

> I have a ragged array hashmap ('int[][char[]] myArray') on which I would
> *like* to be able to lookup the 'int[]' part and do operations on its
> attributes. Eg...
<snip>
> Unfortunately I can't seem to find any way to take a reference to the
> 'int[]' parts I want to manipulate. Operating on the slices produced by the
> above code leaves the original array attributes as they were.

I think you need to either feed the result back into the array, or take a pointer.

    myArray["test"] = myRow;

after the modification.  Alternatively, try

----------
int[]* myRow = &myArray["test"];
if (myRow.length <= idx) myRow.length = idx + 1;
(*myRow)[idx] = 10;
----------

I don't know if this'll actually work, and I'm not in a position to test it at the mo....

Stewart.

-- 
My e-mail is valid but not my primary mailbox, aside from its being the unfortunate victim of intensive mail-bombing at the moment.  Please keep replies on the 'group where everyone may benefit.
July 22, 2004
Thanks for that Stewart. Works a treat. I didn't consider using pointers with my D head on :)

Stewart Gordon wrote:

> Cabal wrote:
> 
>> I have a ragged array hashmap ('int[][char[]] myArray') on which I would *like* to be able to lookup the 'int[]' part and do operations on its attributes. Eg...
> <snip>
>> Unfortunately I can't seem to find any way to take a reference to the 'int[]' parts I want to manipulate. Operating on the slices produced by the above code leaves the original array attributes as they were.
> 
> I think you need to either feed the result back into the array, or take a pointer.
> 
>      myArray["test"] = myRow;
> 
> after the modification.  Alternatively, try
> 
> ----------
> int[]* myRow = &myArray["test"];
> if (myRow.length <= idx) myRow.length = idx + 1;
> (*myRow)[idx] = 10;
> ----------
> 
> I don't know if this'll actually work, and I'm not in a position to test it at the mo....
> 
> Stewart.
> 

July 22, 2004
In article <cdo8mi$lah$1@digitaldaemon.com>, Stewart Gordon says...
>
>I think you need to either feed the result back into the array, or take a pointer.
>
>     myArray["test"] = myRow;
>
>after the modification.  Alternatively, try
>
>----------
>int[]* myRow = &myArray["test"];
>if (myRow.length <= idx) myRow.length = idx + 1;
>(*myRow)[idx] = 10;
>----------

I've been wondering if it might make sense to offer a reference type specifier. So far I can think of two ways of doing it without introducing an additional keyword:

// possibly confusing as this looks like a normal alias call alias int[] myRow = myArray["test"];

// a bit weird but closer in meaning to what we want to do inout int[] myRow = myArray["test"];

In both cases the variable would have to be assigned when declared, and it could not be reassigned afterwords.  ie. it would work just like a reference in C++.


Sean