How to get command line arguments to a table in Lua? - lua

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.

Related

Loop to select which variable to omit from analysis

I have datasets with a large number of variables and I need to run PCA over these datasets with one variable removed each time. Below are 20 variables for an example dataset. I would like to run PCA with one variable removed from each PCA solution. For example, the first PCA solution will include all variables excluding Var_1_GroupA, the second will include all variables excluding Var_2_GroupA, etc. I am familiar with using macros to write loops but unsure how to complete the following task using macros or code in python.
Var_1_GroupA
Var_2_GroupA
Var_1_GroupB
Var_2_GroupB
Var_3_GroupB
Var_1_GroupC
Var_2_GroupC
Var_3_GroupC
Var_4_GroupC
Var_5_GroupC
Var_1_GroupD
Var_1_GroupE
new_Var_1_GroupA
new_Var_1_GroupB
new_Var_1_GroupC
new_Var_2_GroupC
Var_1_GroupF
Var_1_GroupG
Var_1_GroupH
Var_2_GroupH
In the example below I create 10 variables, and then run a simple means command with a different set of variables each time - excluding one of the variables at a time. You can edit the code to match your variables and your analysis code.
data list list/var1 to var10 (10F1).
begin data
1 2 3 4 5 6 7 8 9 9
5 4 3 6 3 8 1 2 5 8
0 8 6 4 2 1 3 5 7 9
end data.
dataset name wrk.
define !loopit (!pos=!cmdend)
!do !a !in(!1)
means
!do !b !in(!1) !if (!b<>!a) !then !b !ifend !doend
.
!doend
!enddefine.
!loopit var1 var2 var3 var4 var5 var6 var7 var8 var9 var10 .
note you vave to list the variable names in the macro call, can't use var1 to var10.
If you run into trouble while adapting this to your exact needs, these are very helpful in debugging macros:
set mexpand=on.
set mprint=on.

Change values in table using coronaSDK in Lua

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

Printing k, v from a defaultdict(list)

I have a program that connects via SSH (Paramiko library) to a Cisco Wireless
LAN Controller (WLC). I then run a 'show client summary' and parse\process
the output to generate a report.
Everything works except the printing.
NOTE: 'e' is a dictionary created with: defaultdict(list)
If I use this:
for k, v in e.items():
print('{:25}'.format(k), end='')
for i in v:
print('{:5}'.format(i), end='')
print("\n")
The output looks like this:
AP Count
------------------------------
AP0027.e3f1.9208 8 7 6
AP70df.2f42.3450 1 1 1
AP25-AthleticOffice 4 4 3
AP70df.2f74.9868 1 1 1
AP70df.2f42.3174 2 2 2
I don't want the extra blank line between the data lines.
But if I simply get rid of the last line: print("\n"),
then I get this format for the output:
AP0027.e3f1.9208 8 7 6AP70df.2f42.3450 1 1 1AP25-AthleticOffice 4 4 3AP70df.2f42.3174 1 1 1AP70df.2f42.3174 2 2 2
No carriage return.
I am either getting zero carriage return or two.
This happens because print() already appends the end character - which is \n by default. You can fix it by printing just an empty string (which is the same as print('', end='\n')):
for k, v in e.items():
print('{:25}'.format(k), end='')
for i in v:
print('{:5}'.format(i), end='')
print('')

Functional impact of declaring local variables via function parameters

In writing some one-off Lua code for an answer, I found myself code golfing to fit a function on a single line. While this code did not fit on one line...
foo=function(a,b) local c=bob; some_code_using_c; return c; end
...I realized that I could just make it fit by converting it to:
foo=function(a,b,c) c=bob; some_code_using_c; return c; end
Are there any performance or functional implications of using a function parameter to declare a function-local variable (assuming I know that a third argument will never be passed to the function) instead of using local? Do the two techniques ever behave differently?
Note: I included semicolons in the above for clarity of concept and to aid those who do not know Lua's handling of whitespace. I am aware that they are not necessary; if you follow the link above you will see that the actual code does not use them.
Edit Based on #Oka's answer, I compared the bytecode generated by these two functions, in separate files:
function foo(a,b)
local c
return function() c=a+b+c end
end
function foo(a,b,c)
-- this line intentionally blank
return function() c=a+b+c end
end
Ignoring addresses, the byte code report is identical (except for the number of parameters listed for the function).
You can go ahead and look at the Lua bytecode generated by using luac -l -l -p my_file.lua, comparing instruction sets and register layouts.
On my machine:
function foo (a, b)
local c = a * b
return c + 2
end
function bar (a, b, c)
c = a * b
return c + 2
end
Produces:
function <f.lua:1,4> (4 instructions at 0x80048fe0)
2 params, 4 slots, 0 upvalues, 3 locals, 1 constant, 0 functions
1 [2] MUL 2 0 1
2 [3] ADD 3 2 -1 ; - 2
3 [3] RETURN 3 2
4 [4] RETURN 0 1
constants (1) for 0x80048fe0:
1 2
locals (3) for 0x80048fe0:
0 a 1 5
1 b 1 5
2 c 2 5
upvalues (0) for 0x80048fe0:
function <f.lua:6,9> (4 instructions at 0x800492b8)
3 params, 4 slots, 0 upvalues, 3 locals, 1 constant, 0 functions
1 [7] MUL 2 0 1
2 [8] ADD 3 2 -1 ; - 2
3 [8] RETURN 3 2
4 [9] RETURN 0 1
constants (1) for 0x800492b8:
1 2
locals (3) for 0x800492b8:
0 a 1 5
1 b 1 5
2 c 1 5
upvalues (0) for 0x800492b8:
Not very much difference, is there? If I'm not mistaken, there's just a slightly different declaration location specified for each c, and the difference in the params size, as one might expect.

How to tell if a Lua line number is a valid execution point (from C/C++)?

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 :)

Resources