Passing variable name over Serial

Hi - After searching around yesterday evening, I'm pretty sure that what I want to do isn't possible on the Arduino, but I'll ask anyhow - it sometimes offers up different ways. Warning - I'm quite rusty and this is quite a simple predicament!

For the purposes of a simplified example, let's say I have 10 LEDs, and have their pins declared thus (together with two other variables for serial)...

const int led1=2;
const int led2=3;
const int led3=4;
.
.
.
const int led10=11;

String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete

Now, I want to pass a command over the serial monitor that turns on/off one of the LEDs. I've got the serialEvent code working so that what I type in a serial monitor line is stored in an inputString variable. So, I might type into the Serial Monitor...

led4_on
led8_off

...and that would be stored in inputString. Now to process the string (and this has the validation chopped out, so this example assumes the command entered is valid...

void loop() {
// process the command when a newline arrives (handled by serialEvent script):
  if (stringComplete) {

      // validation of command in here (all basic stuff)

      String thisLED = inputString.substring(0, inputString.indexOf("_")); // get the characters before the _
      String strAction = inputString.substring(inputString.indexOf("_")+1);  // get the characters after the _
      bool thisAction = true;
      if(strAction=="off") {
        thisAction=false;  // assume they want on unless it says off in the string
      }

      // *************************************
      // perform "thisAction" on "thisLED"  <---the bit I'm struggling with
      // *************************************

   } 
}

At this point, I've got the name of the LED I want to turn off/on stored in thisLED and whether I want it on or off stored in the boolean thisAction. My quick test code just has 10 if statements saying something like...

if(thisLED=="led1") {
   digitalWrite(led1, thisAction);
}
if(thisLED=="led2") {
   digitalWrite(led2, thisAction);
}
.
.
. etc.

...which is a bit cumbersome - 10 if statements. I appreciate that the Switch case is there, but that's still listing all LEDs and performing a match. I hoping this can be done with some kind of pointer/reference, on a couple of lines, but I'm having trouble. After all, I've got the name of the variable I want to get the value for stored in "thisLED". I'm thinking this goes back to the reasons why something like "Eval" isn't available, but fingers crossed.

Is this possible, or am I going about it the wrong way?

Eventually, this is planned to be a compiled program hooked into the SDK of another utility. I'm planning on mapping the SDK's variable names for display lights into the variable names for the LED pins in the Arduino. I can then pass through the variable name of the light that has been triggered and the Arduino will have that mapped to the pin. The only other way I can see is if I store the Arduino's pin map in my compiled program on the PC and pass down the pin numbers I want to turn on and off over serial.

Any help gratefully appreciated, thank you.

You are right in that what you want to do is not possible. Once the program is compiled it loses all concept of variable names.

The way you are doing it is reasonable for small quantities (though I'd stay away from the String library and use C strings and functions, like strcmp()). For larger quantities you might want to consider a couple of arrays (one for pin names, one for pin numbers), or an array with a custom data structure. Then you iterate through the array(s) looking for a match.

Thank you. Yes, two arrays would probably work. I was using it the other day for a different project - not sure why I didn't connect them.

What I'm looking at might get quite large, so I've been keen to get the basics as efficient as possible

Thanks for the tips on strings. Coming from a VBScript and Web Development background I can tell I've still got a lot of reading ahead of me!

Assuming that the string values you're matching against would be hard-coded in your sketch, store them in PROGMEM to save RAM space.

Thank you - will do