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
107
votes
2 answers

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 ipairs(): a = {"one", "two", "three"} for i, v in…
user11071538
106
votes
7 answers

Define default values for function arguments

In the Lua wiki I found a way to define default values for missing arguments: function myfunction(a,b,c) b = b or 7 c = c or 5 print (a,b,c) end Is that the only way? The PHP style myfunction (a,b=7,c=5) does not seem to work. Not that…
ripat
  • 3,076
  • 6
  • 26
  • 38
105
votes
3 answers

For Loop on Lua

My assignment is how to do a for loop. I have figured it out in terms of numbers but cannot figure it out in terms of names. I would like to create a for loop that runs down a list of names. Following is what I have so far: names = {'John', 'Joe',…
SamYoungNY
  • 6,444
  • 6
  • 26
  • 43
104
votes
3 answers

How do I append to a table in Lua

I'm trying to figure out the equivalent of: foo = [] foo << "bar" foo << "baz" I don't want to have to come up with an incrementing index. Is there an easy way to do this?
drewish
  • 9,042
  • 9
  • 38
  • 51
92
votes
6 answers

How to check if two Torch tensors or matrices are equal?

I need a Torch command that checks if two tensors have the same content, and returns TRUE if they have the same content. For example: local tens_a = torch.Tensor({9,8,7,6}); local tens_b = torch.Tensor({9,8,7,6}); if (tens_a EQUIVALENCE_COMMAND…
DavideChicco.it
  • 3,318
  • 13
  • 56
  • 84
89
votes
12 answers

How to fix "NSURLErrorDomain error code -999" in iOS

I've been trying to use Corona SDK's Facebook API to post the score on the game I'm developing on facebook. However, I'm having a problem with it. During the first time I try to post to facebook, I get this error after login and user…
user1597438
  • 2,171
  • 5
  • 35
  • 78
87
votes
17 answers

Check if a file exists with Lua

How can I check if a file exists using Lua?
Yoni
  • 909
  • 1
  • 7
  • 8
86
votes
10 answers

How to merge two tables overwriting the elements which are in both?

I need to merge two tables, with the contents of the second overwriting contents in the first if a given item is in both. I looked but the standard libraries don't seem to offer this. Where can I get such a function?
RCIX
  • 38,647
  • 50
  • 150
  • 207
85
votes
1 answer

How to check if matching text is found in a string in Lua?

I need to make a conditional that is true if a particular matching text is found at least once in a string of text, e.g.: str = "This is some text containing the word tiger." if string.match(str, "tiger") then print ("The word tiger was…
Village
  • 22,513
  • 46
  • 122
  • 163
84
votes
12 answers

Lua - Current time in milliseconds

Is there a common way to get the current time in or with milliseconds? There is os.time(), but it only provides full seconds.
okoman
  • 5,529
  • 11
  • 41
  • 45
84
votes
5 answers

How to iterate through table in Lua?

So, I have a table something along these lines: arr = { apples = { 'a', "red", 5 }, oranges = { 'o', "orange", 12 }, pears = { 'p', "green", 7 } } It doesn't seem like it's possible to access them based on their index, and the values…
Lemony Lime
  • 1,113
  • 3
  • 11
  • 12
82
votes
2 answers

Check if a string isn't nil or empty in Lua

I've some currently some Lua code using the following syntax: if (foo == nil or foo == '') then foo = "some default value" end The goal of the if condition is to test foo is neither an empty string, neither a nil value. Can this code be…
Uskiver
  • 845
  • 1
  • 6
  • 6
78
votes
2 answers

Join tiles in Corona SDK into one word for a Breakout game grid?

I have a game project to re-implement Breakout. I want to display two words, each word on a line. They are joined by the bricks block. Inside, the top line is the first name, aligned left. The bottom line is the last name, aligned right. They are…
nguyentrungkien
  • 461
  • 4
  • 5
78
votes
7 answers

How can I create a secure Lua sandbox?

So Lua seems ideal for implementing secure "user scripts" inside my application. However, most examples of embedding lua seem to include loading all the standard libraries, including "io" and "package". So I can exclude those libs from my…
user150008
77
votes
2 answers

Why use Mongrel2?

I'm confused what purpose Mongrel2 serves/provides that nginx doesn't already do. (Yes, I've read the manual but I must to be too much of a noob to understand how it's fundamentally different than nginx) My current web application stack is: -…
frooyo
  • 1,863
  • 3
  • 19
  • 21