Skip to main content
The 2024 Developer Survey results are live! See the results

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

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.
David Gomes's user avatar
  • 5,725
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....
Jason S's user avatar
  • 188k
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.
RCIX's user avatar
  • 39.2k
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 ...
Dant's user avatar
  • 2,141
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 ...
Cliff's user avatar
  • 11k
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[...
Roman Starkov's user avatar
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 ...
Philipp Lenssen's user avatar
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 ...
Khaled Alshaya's user avatar
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 ...
Wookai's user avatar
  • 21.4k
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"))
Softnux's user avatar
  • 2,650
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 ...
Amber's user avatar
  • 521k
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() ...
Wookai's user avatar
  • 21.4k
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....
stefs's user avatar
  • 18.5k
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 ...
user avatar
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 ...
grigoryvp's user avatar
  • 41.9k

15 30 50 per page
1
2 3 4 5
1522