lua-users home
lua-l archive

Oddity with tinsert (Lua 4.0)

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


If I run the following script:
t = {}
tinsert(t, 3, "hello", 1)
print(t[3])
I get:
1
when I expect
hello
Looking at the source of luaB_tinsert in lbaselib.c, there appears to be a
bug: the first line says:
 int v = lua_gettop(L); /* last argument: to be inserted */
i.e. it always inserts tinsert's *last* argument, not its third.
I was bitten by this because I was trying to execute a statement of the form:
tinsert(t, n, gsub(...)), and was getting the number of replacements rather
than the result of the replacement inserted. Surely this is wrong?
Adding
if (v > 3) v = 3; /* adjust to three args maximum */
as the third line of the function cures the problem. Or have I misunderstood
something?
-- 
http://sc3d.org/rrt/ | egrep, n. a bird that debugs bison

AltStyle によって変換されたページ (->オリジナル) /