www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Associative Arrays

reply Dan <murpsoft hotmail.com> writes:
Walter, what's happening with them?

Last I checked, I couldn't use practically anything of them:

const char[] TEXT_x = "x";
int[char[]] x;

x.length = 1; // doesn't work, no property length
x["hello"] = 1; // doesn't work, array out of bounds
x[TEXT_x] = 1; // doesn't work, array out of bounds

Also, there was a drowned out call by some folks to get static associative
arrays.

What would be the primary blocker for that?  The use of pointers for hashing?

Is it possible to use opIndex etc to override associatives?
Mar 16 2007
next sibling parent reply Carlos Santander <csantander619 gmail.com> writes:
Dan escribió:
 Walter, what's happening with them?
 
 Last I checked, I couldn't use practically anything of them:
 
 const char[] TEXT_x = "x";
 int[char[]] x;
 
 x.length = 1; // doesn't work, no property length
 x["hello"] = 1; // doesn't work, array out of bounds
 x[TEXT_x] = 1; // doesn't work, array out of bounds
 
 Also, there was a drowned out call by some folks to get static associative
arrays.
 
 What would be the primary blocker for that?  The use of pointers for hashing?
 
 Is it possible to use opIndex etc to override associatives?
What exactly are you doing? This compiles and runs successfully with GDC 0.23, and should be the same with DMD (unless something is broken with 1.009): void main () { const char[] TEXT_x = "x"; int[char[]] x; //x.length = 1; x["hello"] = 1; x[TEXT_x] = 1; } And yeah, AAs don't have length. You can get their length with x.keys.length, but you can't set it. -- Carlos Santander Bernal
Mar 16 2007
next sibling parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
Carlos Santander Wrote:

<snip>
 And yeah, AAs don't have length.  You can get their length with 
 x.keys.length, but you can't set it.
I've no idea where you get that idea from. AAs do have length. It's just read-only. Stewart.
Mar 16 2007
parent Carlos Santander <csantander619 gmail.com> writes:
Stewart Gordon escribió:
 Carlos Santander Wrote:
 
 <snip>
 And yeah, AAs don't have length.  You can get their length with 
 x.keys.length, but you can't set it.
I've no idea where you get that idea from. AAs do have length. It's just read-only. Stewart.
That's what I meant, mostly :) -- Carlos Santander Bernal
Mar 16 2007
prev sibling parent Dan <murpsoft hotmail.com> writes:
Carlos Santander Wrote:

 What exactly are you doing? This compiles and runs successfully with GDC 0.23, 
 and should be the same with DMD (unless something is broken with 1.009):
 
 void main ()
 {
      const char[] TEXT_x = "x";
      int[char[]] x;
 
      //x.length = 1;
      x["hello"] = 1;
      x[TEXT_x] = 1;
 }
 
 And yeah, AAs don't have length. You can get their length with x.keys.length, 
 but you can't set it.
Okay, thanks Carlos. I'll have to run the simplified test case, but for Walnut, I couldn't use a Value[char[]] in any of the ways described (Value is a struct). I could declare the associative arrays and handle them, but not populate the associative array with anything or set the length. I thought associative arrays might not be dynamic because it was giving me an Array out of bounds exception when I tried to assign to it. ~~~ Regarding the opIndex, what I meant was perhaps being able to, for example, define an override for line two of: 1 || int[char[]] x; 2 || x["hello"] = 0; It's not clear in documentation whether you can or not, only that you can do it with int's - one would assume in static or dynamic arrays.
Mar 16 2007
prev sibling parent Stewart Gordon <smjg_1998 yahoo.com> writes:
Dan Wrote:

 Walter, what's happening with them?
 
 Last I checked, I couldn't use practically anything of them:
 
 const char[] TEXT_x = "x";
 int[char[]] x;
 
 x.length = 1; // doesn't work, no property length
Of course it doesn't. You can't set the length of an AA: - you can't use it to add elements, because an AA element cannot exist without a key - you can't use it to remove elements, as how would it know which to remove?
 x["hello"] = 1; // doesn't work, array out of bounds
 x[TEXT_x] = 1; // doesn't work, array out of bounds
Works for me (DMD 1.009, Win98SE). Which version and OS are causing a problem for you? <snip>
 Is it possible to use opIndex etc to override associatives?
What do you mean? Stewart.
Mar 16 2007