Pages: [1]   Go Down
Author Topic: Is there a command equivalent to lookup(...) in Arduino?  (Read 347 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 34
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 481
Posts: 18737
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

int answer = 2;

char response = foo [answer];  // will be 'C'
Logged


Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
int answer = 2;

char response = "ABCDE"[answer];
Logged

Pages: [1]   Go Up
Jump to: