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
69
votes
12 answers

Search for an item in a Lua list

If I have a list of items like this: local items = { "apple", "orange", "pear", "banana" } how do I check if "orange" is in this list? In Python I could do: if "orange" in items: # do something Is there an equivalent in Lua?
Jay
  • 1,423
  • 2
  • 11
  • 9
67
votes
16 answers

How do you copy a Lua table by value?

Recently I wrote a bit of Lua code something like: local a = {} for i = 1, n do local copy = a -- alter the values in the copy end Obviously, that wasn't what I wanted to do since variables hold references to an anonymous table not the values…
Jon 'links in bio' Ericson
  • 20,880
  • 12
  • 98
  • 148
67
votes
4 answers

Lua pattern matching vs. regular expressions

I'm currently learning lua. regarding pattern-matching in lua I found the following sentence in the lua documentation on lua.org: Nevertheless, pattern matching in Lua is a powerful tool and includes some features that are difficult to match with…
aurora
  • 9,607
  • 7
  • 36
  • 54
66
votes
3 answers

Lua operators, why isn't +=, -= and so on defined?

This is a question I've been mildly irritated about for some time and just never got around to search the answer to. However I thought I might at least ask the question and perhaps someone can explain. Basically many languages I've worked in utilize…
qrikko
  • 2,483
  • 2
  • 22
  • 35
64
votes
5 answers

Lua os.execute return value

Is it possible to read the following from the local variable in Lua? local t = os.execute("echo 'test'") print(t) I just want to achieve this: whenever os.execute returns any value, I would like to use it in Lua - for example echo 'test' will…
Cyclone
  • 14,839
  • 23
  • 82
  • 114
63
votes
5 answers

Is Lua based primarily on well-established programming-language ideas?

Lua occupies a good place in the space of languages that can be embedded. Are the primary ideas behind Lua's design new ideas from the implementors, or is Lua primarily a well-executed combination of well-established ideas? Comparison of properties…
Charles Stewart
  • 11,661
  • 4
  • 46
  • 85
62
votes
1 answer

Is there a better way to require file from relative path in lua

My directory structure looks like this: |-- ball.lua |-- entity.lua |-- test `-- ball_test.lua I'm using the following code in test/ball_test.lua to require ball.lua from the parent directory: package.path = package.path ..…
Seth Reno
  • 5,350
  • 4
  • 41
  • 44
62
votes
1 answer

Why is LuaJIT so good?

EDIT: unfortunately LuaJIT was taken out of the comparison in the link below. This comparison of programming languages shows that LuaJIT has an over tenfold improvement over the normal Lua implementation. Why is the change so big? Is there something…
Mihai
  • 1,261
  • 2
  • 12
  • 19
62
votes
3 answers

Torch - How to change tensor type?

I created a permutation of the numbers from 1 to 3. th> y = torch.randperm(3 ); th> y 3 2 1 [torch.DoubleTensor of size 3] Now, I want to convert y to a Torch.LongTensor. How can I do that?
una_dinosauria
  • 1,881
  • 2
  • 18
  • 19
59
votes
8 answers

Alternatives to Lua as an embedded language?

I am working on an embedded system running Linux on a DSP. Now we want to make some parts of it scriptable and we are looking for a nice embeddable scripting language. These scripts should integrate nicely with our existing C++ code base, be small…
bastibe
  • 16,551
  • 28
  • 95
  • 126
59
votes
14 answers

List file using ls command in Linux with full path

Many will found that this is repeating questions but i have gone through all the questions before asked about this topic but none worked for me. I want to print full path name of the certain file format using ls command so far i found chunk of code…
Prakash.DTI
  • 913
  • 2
  • 9
  • 19
57
votes
1 answer

Lua String replace

How would i do this? I got this: name = "^aH^ai" string.gsub(name, "^a", "") which should return "Hi", but it grabs the caret character as a pattern character What would be a work around for this? (must be done in gsub)
Frank
  • 573
  • 1
  • 4
  • 4
57
votes
3 answers

What does # mean in Lua?

I have seen the hash character '#' being added to the front of variables a lot in Lua. What does it do? EXAMPLE -- sort AIs in currentlevel table.sort(level.ais, function(a,b) return a.y < b.y end) local curAIIndex = 1 local maxAIIndex =…
Bicentric
  • 1,263
  • 3
  • 12
  • 12
56
votes
5 answers

Load Lua-files by relative path

If I have a file structure like this: ./main.lua ./mylib/mylib.lua ./mylib/mylib-utils.lua ./mylib/mylib-helpers.lua ./mylib/mylib-other-stuff.lua From main.lua the file mylib.lua can be loaded with full path require('mylib.mylib'). But inside…
RocketR
  • 3,626
  • 2
  • 25
  • 38
55
votes
4 answers

Is LuaJIT really faster than every other JIT-ed dynamic languages?

According to the computer language benchmark game, the LuaJIT implementation seems to beat every other JIT-ed dynamic language (V8, Tracemonkey, PLT Scheme, Erlang HIPE) by an order of magnitude. I know that these benchmarks are not representative…
Gabriel Cuvillier
  • 3,617
  • 1
  • 28
  • 35