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

This tag is used for questions about the Lua programming language.

From Lua's About page:

What is Lua?

Lua is a powerful, fast, lightweight, embeddable scripting language.

Lua combines simple procedural syntax with powerful data description constructs based on associative arrays and extensible semantics. Lua is dynamically typed, runs by interpreting bytecode for a register-based virtual machine, and has automatic memory management with incremental garbage collection, making it ideal for configuration, scripting, and rapid prototyping.

The official implementation of Lua is written in clean ANSI C, but a list of several other implementations can be found on the Lua Users Wiki page on Lua Implementations.

The latest official version of Lua is 5.4.1. The previous minor version (5.3.6) is available in the version history on the official site. Lua version numbers are defined as follows: major.minor.bugfix. Minor version increases will often no longer be backwards compatible with prior minor versions.

Lua is certified Open Source software distributed under the terms of the MIT license. As such, Lua is free to use, even for commercial products.

Please note that the official name of this programming language is Lua (a Portuguese word for Earth's moon). It is not an acronym -- it is not spelled LUA.

Learning Lua

Resources

  • Lua.org - The official Lua site.
  • LuaJIT - A fast tracing Just-In-Time compiler for Lua 5.1.
  • LuaRocks - the package manager for Lua modules.
  • ZeroBrane Studio - An IDE and debugger supporting a variety of Lua systems.
  • Lua Development Tools - An Eclipse-based IDE and debugger.
  • LuaDist - Lua modules management system with Virtual Environment capabilities.

Some projects using Lua

  • Torch - A scientific computing framework based on Lua[JIT] with strong CPU and CUDA backends.
  • lua-alchemy - Port of the Lua programming language for ActionScript using Alchemy.
  • Nutria - PHP Standard Library Written in Lua Programming Language
  • Sputnik - A content management system designed for extensibility.

Community

Other places for discussing Lua, beyond the question & answer format of Stack Overflow:

Books

22266 questions
246
votes
15 answers

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
  • 5,644
  • 16
  • 60
  • 103
244
votes
3 answers

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.bar(3, 4) -- 4 return x:foo(3, 4) -- table:…
Jason S
  • 184,598
  • 164
  • 608
  • 970
226
votes
19 answers

Split string in Lua?

I need to do a simple split of a string, but there doesn't seem to be a function for this, and the manual way I tested didn't seem to work. How would I do it?
RCIX
  • 38,647
  • 50
  • 150
  • 207
196
votes
11 answers

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 it?
Dant
  • 1,961
  • 2
  • 12
  • 3
192
votes
10 answers

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[1] = 48 print(#tbl, table.getn(tbl)) -- prints…
Roman Starkov
  • 59,298
  • 38
  • 251
  • 324
184
votes
20 answers

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 can't figure out how. I'm looking for the rough…
Cliff
  • 10,586
  • 7
  • 61
  • 102
183
votes
8 answers

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 with something looking rather "solid", as convex…
Philipp Lenssen
  • 8,818
  • 13
  • 56
  • 77
173
votes
9 answers

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 that is very pleasant to learn and program.…
Khaled Alshaya
  • 94,250
  • 39
  • 176
  • 234
153
votes
5 answers

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 table.contains(table, element) for _, value in…
Wookai
  • 20,883
  • 16
  • 73
  • 86
146
votes
8 answers

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 -- Table is empty end Is there a more…
Amber
  • 507,862
  • 82
  • 626
  • 550
141
votes
6 answers

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
  • 2,440
  • 4
  • 20
  • 21
126
votes
2 answers

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() method, but it only takes the index of the element to…
Wookai
  • 20,883
  • 16
  • 73
  • 86
124
votes
8 answers

subtle differences between JavaScript and Lua

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. There are obvious differences,…
stefs
  • 18,341
  • 6
  • 40
  • 47
117
votes
6 answers

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 str do -- error print( char ) end for i = 1,…
grigoryvp
  • 40,413
  • 64
  • 174
  • 277
112
votes
5 answers

Concatenation of strings in Lua

In many languages you can concatenate strings on variable assignment. I have a scenario, using the Lua programming language, where I need to append the output of a command to an existing variable. Is there a functional equivalent in Lua to the below…
John Mark Mitchell
  • 4,522
  • 4
  • 28
  • 29
1
2 3
99 100