22,981 questions
270
votes
16
answers
455k
views
Lua string to int
How can I convert a string to an integer in Lua?
I have a string like this:
a = "10"
I would like it to be converted to 10, the number.
263
votes
4
answers
80k
views
Difference between . and : in Lua
I am confused about the difference between function calls via . and via :
local x = {
foo = function(a, b) return a end,
bar = function(a,b) return b end
}
return x.foo(3, 4) -- 3
return x....
239
votes
21
answers
402k
views
How to dump a table to console?
I'm having trouble displaying the contents of a table which contains nested tables (n-deep). I'd like to just dump it to std out or the console via a print statement or something quick and dirty but I ...
237
votes
24
answers
489k
views
Split string in Lua
How do I split a string? There doesn't seem to be a built-in function for this.
231
votes
12
answers
263k
views
Why does Lua have no "continue" statement?
I have been dealing a lot with Lua in the past few months, and I really like most of the features but I'm still missing something among those:
Why is there no continue?
What workarounds are there for ...
213
votes
11
answers
401k
views
How to get number of entries in a Lua table?
Sounds like a "let me google it for you" question, but somehow I can't find an answer. The Lua # operator only counts entries with integer keys, and so does table.getn:
tbl = {}
tbl["test"] = 47
tbl[...
192
votes
9
answers
125k
views
Sort points in clockwise order?
Given an array of x,y points, how do I sort the points of this array in clockwise order (around their overall average center point)? My goal is to pass the points to a line-creation function to end up ...
186
votes
9
answers
105k
views
Why do Lua arrays(tables) start at 1 instead of 0?
I don't understand the rationale behind the decision of this part of Lua. Why does indexing start at 1? I have read (as many others did) this great paper. It seems to me a strange corner of a language ...
171
votes
5
answers
325k
views
How to check if a table contains an element in Lua?
Is there a method for checking if a table contains a value ? I have my own (naive) function, but I was wondering if something "official" exists for that ? Or something more efficient...
function ...
169
votes
8
answers
163k
views
Inline conditions in Lua (a == b ? "yes" : "no")?
Is there anyway to use inline conditions in Lua?
Such as:
print("blah: " .. (a == true ? "blah" : "nahblah"))
163
votes
7
answers
137k
views
Most efficient way to determine if a Lua table is empty (contains no entries)?
What's the most efficient way to determine if a table is empty (that is, currently contains neither array-style values nor dict-style values)?
Currently, I'm using next():
if not next(myTable) then
...
150
votes
2
answers
178k
views
What is the difference between pairs() and ipairs() in Lua?
In a for loop, what is the difference between looping with pairs() versus ipairs()?
The Programming in Lua book mentions both, however, both appear to generate similar outputs as shown below:
Using ...
136
votes
2
answers
158k
views
How to remove a lua table entry by its key?
I have a lua table that I use as a hashmap, ie with string keys :
local map = { foo = 1, bar = 2 }
I would like to "pop" an element of this table identified by its key. There is a table.remove() ...
130
votes
6
answers
146k
views
How to iterate individual characters in Lua string?
I have a string in Lua and want to iterate individual characters in it. But no code I've tried works and the official manual only shows how to find and replace substrings :(
str = "abcd"
for char in ...
127
votes
8
answers
50k
views
subtle differences between JavaScript and Lua [closed]
I simply love JavaScript. It's so elegant.
So, recently I have played with Lua via the löve2d framework (nice!) - and I think Lua is also great. They way I see it, those two languages are very similar....