I'm making a game using CoronaSDK and I have a question
I have a table with 72 indexes and another table with 6
I must remove the latest 72 indexes and "prepend" the 6 in another table
Variables:
- blockList (has 72)
- extraLine (has 6)
I'm trying to make this:
function Board:addExtraLine()
-- Change latest to latest-6 until 6
for i=12*6, 7, 1 do
self.blockList[i] = self.blockList[i-6]
end
-- This doesnt work
for i=1, 6, 1 do
self.blockList[i].value = self.extraLine[i].value + 0
self.blockList[i].y = 1
end
self.extraLine = {}
end
I think that I'm changing the C pointer instead of the value, but I have no idea how can I fix this.
Someone can help me?
Solved using table.insert
function Board:addExtraLine()
-- Change latest to latest-6 until 6
for i=12*6, 7, 1 do
self.blockList[i] = self.blockList[i-6]
end
-- This doesnt work
for i=1, 6, 1 do
table.insert(self.blockList, i, self.extraLine[i])
self.blockList[i].y = 1
end
self.extraLine = {}
end
Thanks for this
Related
I'm trying to pass command lines argument as Lua$ lua test.lua 5 1 8 and it gives me the out put as 1 2 3 instead of 5 1 8. The code which I tried is given below, The primary objective is, I need to sort those numbers as 1 5 8. I can use "sort" command only for tables.
a = {}
for i = 1, #arg do
table.insert(a, arg[i])
end
for x in pairs(a)do
print(x)
end
what is missing here ? please advice.
There is no need to build arg. The command-line interpreter lua already does that for you.
$ cat test.lua
for i,v in ipairs(arg) do
print(i,v)
end
$ lua test.lua 5 1 8
1 5
2 1
3 8
Your code does not work because you're printing only the keys in the table (numerical indices in that case), not the values.
This question already has answers here:
Access local variable by name
(2 answers)
Closed 6 years ago.
Just like how we can do this:
a = 3
print(_G['a']) -- 3
I want to be able to do something like this:
local a = 3
print(_L['a']) -- 3
I basically want to be able to access local variables using their names as strings. Is there a table that can do this, perhaps one that can be passed as a function argument? It would be like the this keyword in ActionScript.
This is possible by way of the debug library - namely the getlocal and setlocal functions. If you can't use this library (or access the C API), then you're out of luck.
You can extend your global environment with a specially crafted _L table, that when accessed performs linear lookups of the current set of locals.
Reading a local variable simply finds a matching variable name, and returns its value. Writing to a local variable requires you to discover its index in the stack frame, and then update the value accordingly. Note that you cannot create new locals.
Here's a simple example that works with Lua 5.1 (but not Lua 5.2+).
local function find_local (key)
local n = 1
local name, sname, sn, value
repeat
name, value = debug.getlocal(3, n)
if name == key then
sname = name
sn = n
end
n = n + 1
until not name
return sname, sn
end
_G._L = setmetatable({}, {
metatable = false,
__newindex = function (self, key, value)
local _, index = find_local(key)
if not index then
error(('local %q does not exist.'):format(key))
end
debug.setlocal(2, index, value)
end,
__index = function (_, key)
return find_local(key)
end
})
In use:
local foo = 'bar'
print(_L['foo']) --> 'bar'
_L['foo'] = 'qux'
print(_L['foo']) --> 'qux'
local function alter_inside (key)
local a, b, c = 5, 6, 7
_L[key] = 11
print(a, b, c)
end
alter_inside('a') --> 11 6 7
alter_inside('b') --> 5 11 7
alter_inside('c') --> 5 6 11
You could write this in a different manner, using plain functions instead of the table combined with read / write operations (__index, __newindex).
See §2.4 – Metatables and Metamethods if the above use of metatables is a brand new topic for you.
In Lua 5.2+, you can use the special _ENV tables to adjust your current chunk's environment, but note that this is not the same as using local variables.
local function clone (t)
local o = {}
for k, v in pairs(t) do o[k] = v end
return o
end
local function alter_inside (key)
local _ENV = clone(_ENV)
a = 5
b = 6
c = 7
_ENV[key] = 11
print(a, b, c)
end
alter_inside('a') --> 11 6 7
alter_inside('b') --> 5 11 7
alter_inside('c') --> 5 6 11
As a final note, also consider that this (ab)use of locals might not be the best approach.
You could simply store your variables in a table, when appropriate, to achieve the same results with far less overhead. This approach is highly recommended.
local function alter_inside (key)
-- `ls` is an arbitrary name, always use smart variable names.
local ls = { a = 5, b = 6, c = 7 }
ls[key] = 11
print(ls.a, ls.b, ls.c)
end
alter_inside('a') --> 11 6 7
alter_inside('b') --> 5 11 7
alter_inside('c') --> 5 6 11
Don't dig yourself into a hole trying to solve unnecessary problems.
How Can I tell if line number x in a Lua script will respond to the Lua line hook?
Example:
1 first = 1
2
3 function test ( data )
4 if first == 0 then
5 print ("\r\n")
6 end
7 print(data)
8 --[[
9 first = 0
10 ]]
11 end
12
13 test()
14
Line 2,6,8,9,10,12 and 14 does not call a line hook. After I have loaded and executed the script, can I some how, from C/C++, get a table of the executable line numbers?
lua_getinfo can return a table of valid lines if you include L in what.
Some code sample:
local exec_lines = {}
local function exec_line_counter(event, line)
table.insert(exec_lines, line)$
end
local function count_exec_lines(lua_file)
local external_chunk = loadfile(lua_file)
debug.sethook(exec_line_counter, "l")
external_chunk()
debug.sethook()
-- Removing `debug.sethook()` lines:
table.remove(exec_lines, 1)
table.remove(exec_lines, #exec_lines)
end
count_exec_lines("test.lua")
Output of:
table.sort(exec_lines)
for i, num in ipairs(exec_lines) do
print(num)
end
is
1
3
4
7
11
11 <--- not sure why this duplicates. Lack of return? Or because following tailcall?
13
NOTE: it would log only lines being parsed. In Your test case, it does not cover 5th and 6th line, because first not being 0.
Another way of doing this and solving noted case - just simply parsing Lua source: counting and skipping lines which consists only of Lua comments:
--lines
--[[ blocks ]]
EDIT: ah, shoot, edited Your question of doing this with C/C++. Hooking functions can be done with plain C API too. Might make an example if You didn't get a basic idea from an my answer already made :)
I want to have a if condition within a loop. That is As long as id < 10,
check if Modc_initial is equal to MODC, if true then set d = 12
This is the code I tried bit not working, can anyone please help.
LOOP if (id LT 10)
IF(Modc_initial EQ MODC))
COMPUTE d = 12.
END LOOP.
EXECUTE.
You can either use a one line conditional of the form IF (condition) d = 12. or a multiple line DO IF. Below I provide an example of DO IF adapted to your syntax.
data list free / id MODC Modc_initial.
begin data
1 3 3
2 3 5
12 1 1
end data.
LOOP if (id LT 10).
DO IF (Modc_initial EQ MODC).
COMPUTE d = 12.
END IF.
END LOOP IF (d = 12).
EXECUTE.
Note you had a period missing in your original syntax on the initial LOOP. I also added an end loop condition, otherwise the code as written would just go until the maximum set number of loops per your system.
for i in (0..5)
if(i==0)
i=4
end
puts i
end
In the above program I excepted the output as - 4 5
But instead it is - 4 1 2 3 4 5
So I conclude that loop variable isnt changing. How can change it? Can anyone tell me?
Actually, In my program I need to save the current state of loop and retrive later so that on the next start program resumes from the same point where it was left.
The only way you'll be able to modify the loop variable is to use a while loop:
x = (2..7).to_a
i = 0
while (i < x.length)
i = 4 if i == 0
puts x[i]
i = i + 1
end
Output:
6
7
In a for loop, as you've discovered, you can modify the loop variable and that value will hold for that iteration of the loop. On the next iteration, it will retrieve the next element from the range you specified so your modified value will essentially be overwritten.
There is no way to do that without a hack. The next best thing would be to use next.
x = (0..5).to_a
for i in (0..5)
if(i < 4)
next
end
puts x[i]
end
produces:
4
5
I am not sure how your code relates to the problem you are later mentioning. It looks to me that all you need to do is:
start_pos = load_start_pos || 0 # if not available for load, assume zero
(start_pos..end_pos).each do |i|
if need_to_exit?
save_start_pos i # save for later
break # exit the loop
end
end