lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


>int fnIndex(lua_State * l)
>{
>    if (LUA_TSTRING == lua_type(l, -1))
>    {
>        char * keyname = lua_tostring(l, -1);
>        if (!strcmp(keyname, "KnownKeyName1"))
>            lua_pushnumber(l, somevariable); 
>        else if (!strcmp(keyname, "KnownKeyName2"))
>            lua_pushnumber(l, someothervariable); 
>        ...
>        ...
>        ...
>        else if (!strcmp(keyname, "KnownKeyName999"))
>            lua_pushnumber(l, yetanothervariable);
>        return 1;
>    }
>
>    return 0;
>}
>
>How can I avoid all those strcmps?

If the strings are really all known beforehand, then the fastest thing I
can think of is to write your index tag method in Lua to check for these
strings in a Lua table and only then go to C.

>My original mail contained a
>suggestion for how Lua could eliminate the need for that.

I'll have to check it again.
--lhf