lua , Why does if then return break end not work in Lua inside for loop

Why does if then return break end not work in Lua inside for loop


Tag: lua

I have the following function which checks if the given parameter is found as a key in a key-value table. If that is the case it should return true and break out of the loop. If nothing is found then do nothing.

function checkId(id)
  for k,v in pairs(info) do
    if id == tostring(k) then
      return true
      break -- break out of loop. mission accomplished.

I get an

'end' expected (to close 'do' at line 192) near 'break'

when I try to run this script. What am I missing?


Logically you can't return and break like that.

return exits the function immediately (so you don't need the break).

That specific error is because in lua return has to be the last statement in a block.


How I get the cdata out in c function with variable arguments using luajit?

Here is the code. The aim is to print messages. In PrintC, I would like to get e...but arrives as cdata. How can I unpack that or circumvent that? extern "C" { static int PrintC ( lua_State *L ) { // does not work cdata //executor* e = ( executor*...

Lua search through table

I am working on a game project using Corona SDK and I am running into an issue. I am trying to use string.find() in a for loop to test to determine if a value is in a certain table and if so, add that value to another table. My problem...

Write lua_requiref in lua5.1

I would like to use the function lua_requiref, but in lua5.1. Is there a way to get the effect of lua_requiref in lua 5.1?...

Lua os.tempname

The file names returned by the Lua os.tempname function are weak in two respects Unlike most other languages it is not possible to define a custom prefix for the file name The _6char suffix that is generated can, with brute force but nevertheless, be guessed I have at times used...

Lua obfuscator, how to

I understand this question will appear silly but I'm clueless what to do, which makes it even more silly. I have written a lua script for a game - which works, but I've never actually run any lua with the lua programs(from the site download). I would like to obfuscate...

Luarocks can't install a library

I want to install this library: I do luarocks search lua-dbus There are 0 results. sudo luarocks install lua-dbus doesn't install anything as well. The problem is only with this library, because I can search for other libraries on luarocks, for example luarocks search socket gives plenty of matches....

Reading Lua Table from C++

I have tried many alternatives for this simple thing but could not get it work. I want user to define a table from Lua in the 1st step: a={["something"]=10} -- key=something, value=10 Then, in the second step the user will call from Lua a function designed in C++: b=afunction(a) --...

Reading a table when function has many argument return an error

I'm trying to read an array inside a Lua function in a library that I'm developing. When I call the function with one argument, like xyz = {3, 4, 5} setxyz(xyz) I read the array correctly, while if I add a second argument xyz = {3, 4, 5} setxyz(xyz, 3)...

MAMP or Mac nginx MySQL Lua

I've recently purchased a Mac Mini with the only purpose of being a server. I'm a C programmer, learning Lua and Obj-C at the moment. I'm trying to set up a website, a kind of CV/Personal Promotion site. I've been looking at MAMP for a while however, I would like...

Lua - Is it possible to check if 2 functions are equal?

Is it either possible to get the size of a function in bytes to see if it matches another function similar to C++ sizeof operator, or evaluate two functions some other way to see if they are both equal without actually knowing what the function/s are? Example: local function equals(func1,...

conditional assignment of multiple values

I've got a function that either can take two parameter, or calls another function to retreive these values(semi-defaults in that case). Let's say the first function looks like this: -- the actuall issue function: function foo( param_1, param_2 ) local bar_1, bar_2 = param_1, param_2 or getBar() -- make funny...

Lua in pairs with same order as it's written

Is there any way to loop trough a table like the one below in the same order as it's written? local tbl = { ["hello"] = 1, [2] = 2, [50] = 3, ["bye"] = 4, [200] = 5 } What I mean is that when I use "in pairs"...

Fast way to initialize a tensor in torch7

I need to initialize a 3D tensor with an index-dependent function in torch7, i.e. func = function(i,j,k) --i, j is the index of an element in the tensor return i*j*k --do operations within func which're dependent of i, j end then I initialize a 3D tensor A like this: for...

2D Line reflection on a “mirror”

So I've been working at this on and off for a week, googling and all and I haven't found how to do this. I have a table of "rays" and a table of "lines", and I want the lines to act as mirrors and reflect a ray whenever the ray...

lua - table.concat with string keys

I'm having a problem with lua's table.concat, and suspect it is just my ignorance, but cannot find a verbose answer to why I'm getting this behavior. > t1 = {"foo", "bar", "nod"} > t2 = {["foo"]="one", ["bar"]="two", ["nod"]="yes"} > table.concat(t1) foobarnod > table.concat(t2) The table.concat run on t2 provides no...

horizontal movement in game (rotated player)

In my world I have a character wich is always rotated towards the mouse. It is very logical that when the player moves forward or backward, that the character's x coordinate equals it's x coordinate minus (or plus) the cosine of its x coordinate. And the same for its y...

Convolution Neural Network in torch. Error when training the network

I am trying to base my Convolution neural network upon the following tutorial: The issue is that my images are of different dimensions than those used in the tutorial. (3x200x200). Also I have only two classes. The following are the changes that I made : Changing the dataset to...

Nginx disable or decode url encoding when writing to access log

I have an open resty nginx with lua code that enhance the data and then I write this data to the access log for data analysis. The data that the lua code produce includes double quotes but nginx write it to the access log as \x22. Can I disable this...

Lua in-place initialized table order

I want to be able to do the following: MyObject:SetSize( { 10.0, 20.0 } ) But when I iterate over this on the C-side (SetSize is a C function). The order of the parameters is random. This is the C++ side processing the table: glm::vec2 State::PopVec2() { glm::vec2 v(); lua_pushnil(...

Recursive loop stopping after a short time

Lua in ComputerCraft 1.5 This seems to work but the recursive loops is breaking after 4 or 5 times running. Cannot seem to see why. Am i doing something incredibly wrong here? Full Code Snippet for the loop: x = 1 function loop() if x > 0 then getTarg() derp1()...

Supporting “recursive objects” in lua

I'm fairly new to lua and have the following problem with an assignment from a class: We currently extend lua to support objects and inheritance. The Syntax for that is Class{'MyClass', attribute1 = String, attribute2 = Number } Class{'MySubClass', MyClass, attribute3 = Number } This works perfectly fine. The real...

ROBLOX Lua Error in script: '=' expected near ''

Hello I am writing a scipt on ROBLOX and I have encountered a problem. function showVictoryMessage(playerName) local message ="Message") message.Text = playerName .." has won!" message.Parent = game.Workspace wait (2) message.Destroy() end Upon running this function, or more specifically the "message.Destroy" command, I get the error: Error in script:...

How to Draw Circle, incrementally, to create an animated fill effect (using Corona)

I'm having trouble even figuring out where to start with this. ANY help would be highly appreciated! Using the Corona SDK I want to draw a circle that will slowly fill as a percentage increases. The fill effect will follow the path of the circle, going anti-clockwise until the entire...

iterating over a table passed as an argument to a function in lua

I am trying using the for _ in pairs() notation to iterate over a table within a function, but if I type anything, even gibberish like print('asdgfafs'), nested inside the for loop, it never gets printed. Code: record = {bid1,bid2,bid3} bid1 = {bidTime = 0.05,bidType = 'native'} bid2 = {bidTime...

How can I convert a character code to a string character in Lua?

How can I convert a character code to a string character in Lua? E.g. d = 48 -- this is what I want str_d = "0" ...

Lua error loading module 'libpng' (Torch, MacOSX)

How do I make libpng load properly in Lua? I am running Lua/Torch in iTorch Notebook in Mac OSX 10.10.3, where other basic functions in Lua work, such as plotting and calculations. brew install libpng Warning: libpng-1.6.17 already installed If I run: require 'nn'; require 'image'; itorch.image(image.lena()) error loading module...

storyboard.removeScene(“scene name”) is not working

I am new to corona sdk and trying to build sample in it. i want to get back to main screen automatically after some time by destroying my scene(i mean that scene destroys itself after some particular time and user get back to main screen).I know that for some this...

How to iterate through a table in its exact order?

If i try to output this table, they are looped through in the false order: local letters = {DIN1="hi", AIN1= "my", AIN2 ="name", DIN2="is"} for name, value in pairs(letters) do print(name,value) end Expected Output: DIN1 hi AIN1 my AIN2 name DIN2 is Output: AIN1 my DIN2 is DIN1 hi AIN2...

Lua: how to check whether a process is running

I would like to start a process from Lua and monitor whether the process is still running. [EDIT] I know starting can be achieved by os:execute, but this is blocking. I would like to find a way to start a process non-blocking and monitor whether it still runs....

Redis Cluster: Find which master holds particular key/slot

I'm using Redis Cluster, and some keys have a special prefix {foo}, so redis puts them in one slot. I do this because i want to run some lua scripts against these keys (i can do this if i login to the master as a single instance). In my setup...

Corona sdk not view random text

I'm have a problem because when I touch my button sometimes my text doe not appear and I do not understand what is wrong. Code: function randomText(event) display.remove(mmDis) local a = {"Cristiano ronaldo jest najlepszy!", "messi jest dobry!","lewandowski jest ok","diego lopez to bramkarz realu"} com = (a[math.random(1,#a)]) local mmDis =...

Call download lua script in app

I am trying to develop a game by using the c++ with lua, and I am using the quick-cocos2d-x engine to help me finish that. The problem is: I need user to download the lua scripts into their sdcard on their iphone/android devices, so that I can call the function...

Lua function selector

I have 3 functions. When the user presses the 'E' key I want it to select one of the functions (at random), I feel like it has something to do with math.random but I can't figure it out.

How do I do user inputs on the same line in Lua?

Here is my code: while true do opr = txt = if opr == "print" then print(txt) else print("wat") end end What I'm trying to do is make it where you type print and then whatever you want like this: print text And it'll print text but I...

Computercraft Evaluate and Execute relative to ticks

For the sake of writing fast turtle programs, I am wondering how, in relation to ticks, is code executed? Is there a tick penalty for executing functions or evaluating other Lua statements in addition to the time it takes the turtle to move? In other words, does it take the...

Degree value to two float variables

I need help creating a function to convert an angle to two float variables. Here are some examples of the outputs: 0 degrees = {1, 0} ~31.0760 degrees = {~0.92823970, ~0.37198266} 45 degrees= {~0.70707070, ~0.70707070} 90 degrees = {0, 1} 135 degrees = {~-0.707070707, ~0.707070707} 180 degrees = {-1, 0}...

Run Lua script from Python

Suppose I have a Lua script that contains 2 functions. I would like to call each of these functions with some arguments from a Python script. I have seen tutorials on how to embed Lua code in Python and vice versa using Lunatic Python, however, my Lua functions to be...

Yaw, pitch, and roll rotations to six float variables

I need help creating a function to convert three angles (in degrees, yaw pitch and roll) to six float variables. How would I go about making a function output these floats? {0, 0, 0} = {1, 0, 0, -0, -0, 1} {45, 0, 0} = {0.70710676908493, 0.70710676908493, 0, -0, -0,...

Lua import strings from file

I can't load my user.db into Lua. user.db contains: user1="XYZ1" user2="Xzy2" .... admin="ZXY" Some Bash and Python scripts use the same db. For Bash e.g. I used the source user.db command, but I don't have a clue for lua. Should be something similiar outside?! I tried the require function but...

Return multiple values to a function, and access them separately in Lua?

If I have a function that returns multiple values, how can I access those values separately? Something like table[i]. angles = function() x = function() local value = 0 return value end y = function() local value = 90 return value end z = function() local value = 180 return...

Multiline function tables in lua

I cannot seem to find any other online help about creating a table which contains functions which are multiline. For example, here is a snippit of code from the lua wiki. action = { [1] = function (x) print(1) end, [2] = function (x) z = 5 end, ["nop"] =...

Awesome desktop manager widgets

I came upon this problem before, but only now it really renders awesome desktop manager useless. I was searching how to connect to Wifi with awesome. Found Gigamo Wifi Widget. Cool, how do I eat it? The awesome wiki entry on widgets does not really answer this question. I know...

Trouble connecting to ESP8266 NodeMCU Server

I have been having trouble connecting to my ESP-01 Wifi module running NodeMCU. I have set up a simple server, but whenever I try to connect to the IP Address via my browser, my browser times out. I know I am making connection to the device because I can see...

Erlang spawning large amounts of C processes

I've been looking into how I could embed languages (let's use Lua as an example) in Erlang. This of course isn't a new idea and there are many libraries out there that can do this. However I was wondering if it was possible to start a Genserver with state which...

Luajit: accessing objects of type cdata from C or C++

All is in the title... Apparently it is not possible to convert cdata to userdata, and I couldn't find any function in the C api to push/retrieve cdata to/from the stack. Maybe there's a simple and recommended way to do it that I haven't noticed? Thanks for your help.

Torch Lua: Why is my gradient descent not optimizing the error?

I've been trying to implement a siamese neural network in Torch/Lua, as I already explained here. Now I have my first implementation, that I suppose to be good. Unfortunately, I'm facing a problem: during training back-propagation, the gradient descent does not update the error. That is, it always computes the...

Write to file using Lua FFI

I am trying to use Lua FFI using LuaJIT to append some text to a file, but I am not very knowledgable of C, so I have some trouble. This is the code: local ffi = require "ffi" ffi.cdef[[ typedef int __kernel_ssize_t; typedef __kernel_ssize_t ssize_t; ssize_t write(int fildes, const void...

Lua C++ Table Iteration

I am having a slight confusion of how lua_next really works. User defines a table: a={["a1"]=20,["a2"]=30} I want to print this table with a C++ code: inline int lua_print(lua_State* L) { wxString wxReturnStr=wxEmptyString; wxString tempString=wxEmptyString; int nargs = lua_gettop(L); for (int i=1; i <= nargs; i++) { int type =...

Lua - Function That Creates Detours

I need to make several detours to various functions and doing it one by one is just not an option. I'm looking for a function that will take a table, ideally, and this table is class. Loop through it and for each key, value pair that is a function make...

Sorting 2D boxes into larger boxes to fill in the most efficient and complete manner

Background: Hello, so I've been working on this function for a bit and I'm stumbling. Ultimately, what I'm trying to accomplish is to have an application where a user inputs larger boxes, and also inputs a list of smaller boxes. (All 2 dimensional, by the way). The program then processes...