469,568 Members | 1,757 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,568 developers. It's quick & easy.

Lookup table...if / else vs. switch vs. hash?

cjl
Hey all:

I need to get some user input which will be a six digit number. I will
split it into two character pairs. Each pair will need to be "looked
up", for example:

if firstPair == '01' then variable = "Monkey";
else if firstPair == '02' then variable = "Chicken";
etc..
So I think I could do this with if / else statements chained together,
and I think I could do this with a switch statement.

I remember someone showing a way to do this with a hash, but I can't
find it anywhere.

Is there a javascript idiom that is more elegant for this problem than
if / else or switch?

-CJL

Oct 13 '05 #1
2 2129
On 13/10/2005 13:26, cjl wrote:

[snip]
if firstPair == '01' then variable = "Monkey";
else if firstPair == '02' then variable = "Chicken";
etc..
A lookup table would be the nicest solution:

var pairs = { '01' : 'Monkey',
'02' : 'Chicken' },
variable = pairs[firstPair];

[snip]
I remember someone showing a way to do this with a hash, but I can't
find it anywhere.


Note that the object above, pairs, is not a hash table. It's just an
object that's being used like a lookup table, where property names are
mapped to values.

[snip]

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Oct 13 '05 #2
cjl
Mike:

Thanks. That is exactly what I was looking for.

I guess I may have been using the terms lookup table and hash
incorrectly, but that's what I get for trying to sound like I know
about computer programming using terms that I've seen on Slashdot.

-CJL

Oct 13 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by John Collyer | last post: by
3 posts views Thread by my-wings | last post: by
21 posts views Thread by Johan Tibell | last post: by
9 posts views Thread by sam_cit | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.