Questions tagged [lua]
Lua is a powerful, fast, lightweight, embeddable scripting language. It is dynamically typed, runs by interpreting bytecode, and has automatic garbage collection. Its speed is one of the main reasons it is widely used by the machine learning community. It is often referred to as an "extensible extension language".
22,826
questions
262
votes
16
answers
421k
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.
254
votes
3
answers
71k
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....
232
votes
22
answers
446k
views
Split string in Lua
How do I split a string? There doesn't seem to be a built-in function for this.
214
votes
12
answers
229k
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 ...
210
votes
21
answers
353k
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 ...
198
votes
11
answers
364k
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[...
188
votes
9
answers
119k
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 ...
179
votes
9
answers
92k
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 ...
162
votes
5
answers
294k
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 ...
160
votes
7
answers
133k
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"))
155
votes
8
answers
124k
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
...
133
votes
2
answers
142k
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() ...
127
votes
8
answers
49k
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....
127
votes
2
answers
140k
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 ...
125
votes
6
answers
134k
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 ...