How do I append to a table in Lua
You are looking for the insert function, found in the table section of the main library. foo = {} table.insert(foo, “bar”) table.insert(foo, “baz”)
You are looking for the insert function, found in the table section of the main library. foo = {} table.insert(foo, “bar”) table.insert(foo, “baz”)
pairs() and ipairs() are slightly different. pairs() returns key-value pairs and is mostly used for associative tables. key order is unspecified. ipairs() returns index-value pairs and is mostly used for numeric tables. Non numeric keys in an array are ignored, while the index order is deterministic (in numeric order). This is illustrated by the following … Read more
As other answers have said, the string concatenation operator in Lua is two dots. Your simple example would be written like this: filename = “checkbook” filename = filename .. “.tmp” However, there is a caveat to be aware of. Since strings in Lua are immutable, each concatenation creates a new string object and copies the … Read more
Your problem is simple: names = {‘John’, ‘Joe’, ‘Steve’} for names = 1, 3 do print (names) end This code first declares a global variable called names. Then, you start a for loop. The for loop declares a local variable that just happens to be called names too; the fact that a variable had previously … Read more
If you want named arguments and default values like PHP or Python, you can call your function with a table constructor: myfunction{a,b=3,c=2} (This is seen in many places in Lua, such as the advanced forms of LuaSocket’s protocol modules and constructors in IUPLua.) The function itself could have a signature like this: function myfunction(t) setmetatable(t,{__index={b=7, … Read more
In lua 5.1, you can iterate of the characters of a string this in a couple of ways. The basic loop would be: for i = 1, #str do local c = str:sub(i,i) — do something with c end But it may be more efficient to use a pattern with string.gmatch() to get an iterator … Read more
No, setting the key’s value to nil is the accepted way of removing an item in the hashmap portion of a table. What you’re doing is standard. However, I’d recommend not overriding table.remove() – for the array portion of a table, the default table.remove() functionality includes renumbering the indices, which your override would not do. … Read more
Some more differences: Lua has native support for coroutines. UPDATE: JS now contains the yield keyword inside generators, giving it support for coroutines. Lua doesn’t convert between types for any comparison operators. In JS, only === and !== don’t type juggle. Lua has an exponentiation operator (^); JS doesn’t. JS uses different operators, including the … Read more
Sure: print(“blah: ” .. (a and “blah” or “nahblah”))
Your code is efficient but wrong. (Consider {[false]=0}.) The correct code is if next(myTable) == nil then — myTable is empty end For maximum efficiency you’ll want to bind next to a local variable, e.g., … local next = next … … if next(…) … (When next is local, the code finds primitive function next … Read more