Go Down

Topic: Is there a command equivalent to lookup(...) in Arduino? (Read 426 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
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy