[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: strings with length 0 "disappear" in table
- From: startx <startx@...>
- Date: Wed, 7 Dec 2011 22:00:27 +0000
hi
while i was cleaning some strings ( trimming etc ) before inserting
them into a table, i ran into something which suprised me:
-- SNIPPET Lua 5.1.4
s = " "
t = {}
s = string.gsub(s, "^%s*(.-)%s*$", "%1") -- ( trimming the string )
print(#s) -- 0
print(s) -- empty line
print(type(s)) -- type is "string"
table.insert(t,s)
print(t.s) -- nil
print(type(t.s)) -- nil
d = t.s
print(#d) -- ERROR
--- END SNIPPET
normally, before inserting the string i would do something like
if s then
table.insert(t,s)
end
to make sure i do not push nil into the table, but this would fail
here so when i tried to retrieve the string later i ran into an error.
my questions:
1) wouldnt it make some more sense that a string of length 0 becomes nil
in the first place ( i am not sure how C handles that )
2) the fact that i insert a value type string into a table and get nil
back from it ( hence making it magically disapear ) worries me.
is this deliberate?
startx