Converting one array to both string and variable assignment

Greetings everyone, creating my first project and I'm struggling to find a usable method of having one array provide both string names and assign a data variable. This is a working function that writes the current values from a range of sensors to individual csv files. It is called after sensor data has been updated:

// strings for file location
String dir = "/mnt/sd/";
String format = ".csv";
// Sensor data
int temp;
int hum;
int light;
int moist;
int soil;
// Array of sensor names
char* Sensors[] = {"temp","hum","light","moist","soil",};
void writeLog() { // Write data to log files
  int SensorData[32] = {temp, hum, light, moist, soil}; // array of current sensor data values
  for (int s = 0; s <= 4; s++) { // loop through available sensors
    String cur = Sensors[s]; // string for current sensor name
    String curTime = time(); // grab the time
    curTime.trim(); // and knock off the newline
    String curFile = dir + cur + format; // concat filename string
    int curLen = curFile.length() + 1; // Dynamic buffer length with null terminator
    char curDest[curLen]; // prepare buffer
    curFile.toCharArray(curDest, curLen); // convert to a character array
    File dataFile = FileSystem.open(curDest, FILE_APPEND); // open file to write log
    if (dataFile) { // assuming we can open the file
      dataFile.print(curTime); // timestamp
      dataFile.print(","); // comma
      dataFile.println(SensorData[s]); // sensor data value
      dataFile.close(); // close file
    }
  }
}

Note there are two arrays - one for the string value name of the sensor (Sensors) and one for the current integer values of the sensors (SensorData). This code only works because two arrays of different types are kept manually identical.

So far I have been unable to condense them into one array that can be expanded into both string and integer values. I have tried multiple ways of converting but consistently get stuck on invalid type errors. It also seems I cannot initialise a 3D array of mixed type.

What methods would you use to solve this problem and only maintain one array to provide both data types?

Array of structs

Yes. I agree with the comment of the other user. You need to define a array of struct. You can find in the link:
http://www.cplusplus.com/doc/tutorial/structures/

what is a struct and how it's used.

Beautiful, just what I was after. Working variation:

// strings for file location
String dir = "/mnt/sd/";
String format = ".csv";
// Sensor data
int temp;
int hum;
int light;
int moist;
int soil;
// Lets try an array of structs
struct Sensors { // define the sensor array
  String label; // sensor label
  int data; // sensor data
} Sensors; //object name       
void writeLog() { // Write data to log files
struct Sensors active[5] = {{"temp", temp}, {"hum", hum}, {"light", light}, {"moist", moist}, {"soil", soil},}; // active array 
    String curTime = time(); // grab the curren time
    curTime.trim(); // and chop the newline
  for (int s = 0; s <= sizeof(active); s++) { // loop through each sensor
    String cur = active[s].label; // grab the active label
    String curFile = dir + cur + rID + format; // concat string to write
    int curLen = curFile.length() + 1; // Dynamic buffer length with null terminator
    char curDest[curLen]; // prepare buffer
    curFile.toCharArray(curDest, curLen); // convert string to character array
    File dataFile = FileSystem.open(curDest, FILE_APPEND); // open the log file
    if (dataFile) { // assuming we can...
      dataFile.print(curTime); // time
      dataFile.print(","); // comma
      dataFile.println(active[s].data); // Data value
      dataFile.close(); //close file
    }
  }
}

Thanks for the pointer :slight_smile:

I like to think of structure arrays as "arrays for adults"

econjack:
I like to think of structure arrays as "arrays for adults"

A bit steamy ??

...R