Go Down

Topic: Is there a command equivalent to lookup(...) in Arduino? (Read 417 times) previous topic - next topic

RoboticsProfessor

I'm thinking of something like...

answer = 2;
character = lookup('A', 'B', 'C', 'D', 'E');

Printing character would show 'C' (or maybe 'B' depending on how you index the lookup items).

The closest thing I can think of is to use a table (an array of numbers or characters), then walk through the array with a loop until the desired object is found.

Nick Gammon

You want the second (er, third) item? You don't need to walk it then:

Code: [Select]
char foo [] = { 'A', 'B', 'C', 'D', 'E' };

int answer = 2;

char response = foo [answer];  // will be 'C'
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

lloyddean

As long as the array is of ASCII characters the following also works.

Code: [Select]

int answer = 2;

char response = "ABCDE"[answer];


Go Up