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".
1,696
questions
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....
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 ...
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.
80
votes
7
answers
37k
views
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 "...
6
votes
1
answer
16k
views
How do I de-obfuscate a Lua script?
I have some Lua code that I suspect is obfuscated. How do I go about de-obfuscating it?
I believe the code is obfuscated because it looks very different from normal Lua code, but I know it is valid ...
71
votes
16
answers
113k
views
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 ...
91
votes
13
answers
129k
views
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 ...
20
votes
5
answers
35k
views
Casting between void * and a pointer to member function
I'm currently using GCC 4.4, and I'm having quite the headache casting between void* and a pointer to member function. I'm trying to write an easy-to-use library for binding C++ objects to a Lua ...
30
votes
3
answers
16k
views
Why does Lua's length (#) operator return unexpected values?
Lua has the # operator to compute the "length" of a table being used as an array.
I checked this operator and I am surprised.
This is code, that I let run under Lua 5.2.3:
t = {};
t[0] = 1;
t[1] = ...
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[...
72
votes
12
answers
168k
views
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 ...
90
votes
10
answers
108k
views
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 ...
52
votes
12
answers
84k
views
Safely remove items from an array table while iterating
This question is similar to How can I safely iterate a lua table while keys are being removed but distinctly different.
Summary
Given a Lua array (table with keys that are sequential integers ...
29
votes
4
answers
137k
views
Generating uniform random numbers in Lua
I am programming a Markov chain in Lua, and this requires me to uniformly generate random numbers. Here is a simplified example to illustrate my question:
pickRandomArrayItem = function(x)
local r ...
25
votes
7
answers
59k
views
Associatively sorting a table by value in Lua
I have a key => value table I'd like to sort in Lua. The keys are all integers, but aren't consecutive (and have meaning). Lua's only sort function appears to be table.sort, which treats tables as ...