lua-users home
lua-l archive

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


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"

print(t.s)          -- nil
print(type(t.s)) -- nil

d = t.s
print(#d)        -- ERROR


normally, before inserting the string i would do something like

if s then

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?