Long IF tree with strings
        Posted  
        
            by DalGr
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by DalGr
        
        
        
        Published on 2010-03-26T21:09:50Z
        Indexed on 
            2010/03/26
            21:13 UTC
        
        
        Read the original article
        Hit count: 286
        
I have a C program which uses Lua for scripting. In order to keep readability and avoid importing several constants within the individual Lua states, I condense a large amount of functions within a simple call (such as "ObjectSet(id, "ANGLE", 45)"), by using an "action" string.
To do this I have a large if tree comparing the action string to a list (such as "if(stringcompare(action, "ANGLE") ... else if (stringcompare(action, "X")... etc")
This approach works well, and within the program it's not really slow, and is fairly quick to add a new action. But I kind of feel perfectionist. Is there a better way to do this in C?
And having Lua in heavy use, maybe there is a way to use it for this purpose? (embedded "chunks" making a dictionary?) Although this part is mostly curiosity.
© Stack Overflow or respective owner