lua-users home
lua-l archive

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


On Wed, 7 Dec 2011 22:00:27 +0000
startx <startx@plentyfact.org> wrote:

> hi      
> 
> while i was cleaning some strings ( trimming etc ) before inserting
> them into a table, i ran into something which suprised me:
> 

OOPS sorry, my fault ( to late today )
of course i should have retrieved t[1] , not t.s

pls ignore my question about the table ... : (

nevertheless my question about the string being length 0 itself,
isnt it somehow weired to have a string which is 0 characters long?

startx


> -- 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
>