Can I create associate arrays?

Hi all...

Instead of a basic number-indexed array, I'd love to be able to use a hash map or associative array, so I could refer to items by key value name instead of index. Any way to do this with Arduino? I don't see it mentioned in the documentation.

The Arduino library does not have sophisicated collections like hash maps. The best is probably an array :)

Not many 8-bit embedded systems do have collections. Many times there is not a call for such data structures and they can wasteful of RAM. Some suggestions in no particular order: - Write an Arduino collection library and post it - Think about whether you really need a hash map or not - Post your real problem so we can suggest other smaller and/or cheaper options.

Just because I love to program and occationaly help fellow arduinoers, I’ve started on a HashMap library.
http://www.arduino.cc/playground/Code/HashMap

Example:

#include <HashMap.h>

//define the max size of the hashtable
const byte HASH_SIZE = 5;
//storage
HashType<char*,int> hashRawArray[HASH_SIZE];
//handles the storage [search,retrieve,insert]
HashMap<char*,int> hashMap = HashMap<char*,int>( hashRawArray , HASH_SIZE );

void setup(){
//setup hashmap
hashMap0;
hashMap1;
hashMap2;
hashMap3;
hashMap4;

Serial.begin(9600);
Serial.println( hashMap.getIndexOf(“test”),DEC );
Serial.println( hashMap.getValueOf(“test”) );

hashMap.debug();
}

void loop(){/nothing to loop/}

1 //getIndexOf( "test" );
200 //getValueOf( "test" );
name - 18
test - 200
qwer - 1234
abc - 123
AlphaBeta - 20

Do not hesitate to contact me if you can think of something to add to the library.

[ BEWARE, this library does not really work with strings as hash keys, not yet anyway. ]
:slight_smile:

Thanks for the input everybody! I'm building an Arduino based electro-mechanical orrery (a device that shows the positions of the planets). It won't be connected to the Internet, but it will have a real time clock chip and data files containing the the position info for the planets. I thought it could be easier if I could access that data via a hashmap keyed to the current time.

So for instance, I could do something like "azmiuth = saturn[200907250900];" and get back where Saturn is at 9AM on July 25th.

AlpaBeta, I'll definitely take a look at your library, thanks!

I fully realize that with all of the data I may easily over-flow the Arduino's storage anyway. So I'm also looking into having the data on an SD card and searching through the card to retrieve the needed info every minute or so.

Off to browse the forums for SD reader links.