0

Which languages support non-scalar associative array keys?

I want to make an array like:

[key1,key2,key3,key4]=>[object]

I guess I'd be satisfied if the multiple keys had to each be scalars, although bonus points if they can be any data type.

CommaToast
  • 11,370
  • 7
  • 54
  • 69

2 Answers2

1

What you are looking for is called hash tables (or hashmaps). You can implement them in most languages. Some languages already have support for hash tables like c++, java, lisp, python ...

Here are some references for some languages:

Also, from personal experience I found out that they are extreamly easy to work in lisp.

Luis Alves
  • 1,286
  • 12
  • 32
  • Ah and in fact PHP has it, SplObjectStorage class. Woot. http://php.net/manual/en/class.splobjectstorage.php – CommaToast Nov 21 '14 at 19:03
  • Actually I'm not sure that SplObjectStorage would work either, that's another question though. – CommaToast Nov 21 '14 at 19:16
  • In php you can index arrays using strings (don't know if this works for you). Here's a link http://stackoverflow.com/questions/6841379/is-there-java-hashmap-equivalent-in-php – Luis Alves Nov 21 '14 at 19:43
  • Yes but what I want to do is index arrays using arrays, so that any of the members of the array can be used to navigate the index. So like if an array contains ['foo',400] and that node points to the next node that contains 'bar' then array[400] will give 'bar' and so will array['foo']. – CommaToast Nov 21 '14 at 19:48
0

I don't know of any that support them directly. Perl does allow multiple scalar keys in its associative arrays via $var{$key1, $key2} but all that does is automatically concatenate the two values into a larger one and is equivalent to $var{"$key1$;$key2"}. $; is "\034" so there will be unexpected collisions if your key strings contain that value.

The same trick could be applied in any language by serializing the more complex data type into a single string.

George Phillips
  • 4,564
  • 27
  • 25