Arduino:
Using associative arrays

How to:

Arduino, strictly speaking, doesn’t have built-in support for associative arrays as you’d find in higher-level languages. But, fear not. We can get crafty using structures and arrays to mimic this functionality. Here’s a simple example to create a basic “associative array” for storing and accessing temperatures for different cities.

First, define a structure to hold the city (key) and its temperature (value):

struct CityTemperature {
  String city;
  float temperature;
};

Next, initialize an array of CityTemperature objects:

CityTemperature temperatures[] = {
  {"New York", 19.5},
  {"Los Angeles", 22.0},
  {"Chicago", 17.0}
};

Here’s how you can access and display the temperature of a specific city:

void setup() {
  Serial.begin(9600);
  for(int i = 0; i < 3; i++) {
    if(temperatures[i].city == "Los Angeles") {
      Serial.print("The temperature in Los Angeles is: ");
      Serial.println(temperatures[i].temperature);
    }
  }
}

void loop() {
  // Nothing here for now.
}

Running this code would give you the output:

The temperature in Los Angeles is: 22.0

Deep Dive

Historically, programming languages like C and C++ (from which Arduino syntax is derived) didn’t come with built-in associative arrays, leading to workarounds like the one shown above. This approach is relatively simple but scales poorly as the data size increases due to its O(n) lookup time.

Languages such as Python offer dictionaries, and JavaScript has objects for this purpose, both of which are far more efficient for managing key-value pairs. In Arduino, when performance and efficiency become critical, developers might opt for more specialized data structures, like hash tables, implemented via libraries.

Although Arduino doesn’t natively support associative arrays, the community has developed libraries like HashMap that can be added to your project to provide similar functionality with better performance than a DIY approach. These libraries typically offer a more elegant and efficient means of managing associative arrays, especially for more complex projects.