Lua-error driving me crazy - lua

I keep getting this error and I cannot find it. Please help.
LUA ERROR: Cannot load buffer.
[string "LuaMacros script"]:191: '}' expected (to close '{' at line 85) near '['
Here is the script:
--Start Script
sendToAHK = function (key)
--print('It was assigned string: ' .. key)
local file = io.open("C:\\Users\\TaranWORK\\Documents\\GitHub\\2nd-keyboard-master\\LUAMACROS\\keypressed.txt", "w") -- writing this string to a text file on disk is probably NOT the best method. Feel free to program something better!
--Make sure to substitute the path that leads to your own "keypressed.txt" file, using the double backslashes.
--print("we are inside the text file")
file:write(key)
file:flush() --"flush" means "save"
file:close()
lmc_send_keys('{F24}') -- This presses F24. Using the F24 key to trigger AutoHotKey is probably NOT the best method. Feel free to program something better!
end
local config = { -- this is line 85
[45] = "insert",
[36] = "home",
[33] = "pageup",
[46] = "delete",
[35] = "end",
[34] = "pagedown",
[27] = "escape",
[112] = "F1",
[113] = "F2",
[114] = "F3",
[115] = "F4",
[116] = "F5",
[117] = "F6",
[118] = "F7",
[119] = "F8",
[120] = "F9",
[121] = "F10",
[122] = "F11",
[123] = "F12",
[8] = "backspace",
[220] = "backslash",
[13] = "enter",
[16] = "rShift",
[17] = "rCtrl",
[38] = "up",
[37] = "left",
[40] = "down",
[39] = "right",
[32] = "space",
[186] = "semicolon",
[222] = "singlequote",
[190] = "period",
[191] = "slash",
[188] = "comma",
[219] = "leftbracket",
[221] = "rightbracket",
[189] = "minus",
[187] = "equals",
[96] = "num0",
[97] = "num1",
[98] = "num2",
[99] = "num3",
[100] = "num4",
[101] = "num5",
[102] = "num6",
[103] = "num7",
[104] = "num8",
[105] = "num9",
[106] = "numMult",
[107] = "numPlus",
[108] = "numEnter" --sometimes this is different, check your keyboard
[109] = "numMinus",
[110] = "numDelete",
[111] = "numDiv",
[144] = "numLock", --probably it is best to avoid this key. I keep numlock ON, or it has unexpected effects
[192] = "`", --this is the tilde key just before the number row
[9] = "tab",
[20] = "capslock",
[18] = "alt",
[string.byte('Q')] = "q",
[string.byte('W')] = "w",
[string.byte('E')] = "e",
[string.byte('R')] = "r",
[string.byte('T')] = "t",
[string.byte('Y')] = "y",
[string.byte('U')] = "u",
[string.byte('I')] = "i",
[string.byte('O')] = "o",
[string.byte('P')] = "p",
[string.byte('A')] = "a",
[string.byte('S')] = "s",
[string.byte('D')] = "d",
[string.byte('F')] = "f",
[string.byte('G')] = "g",
[string.byte('H')] = "h",
[string.byte('J')] = "j",
[string.byte('K')] = "k",
[string.byte('L')] = "l",
[string.byte('Z')] = "z",
[string.byte('X')] = "x",
[string.byte('C')] = "c",
[string.byte('V')] = "v",
[string.byte('B')] = "b",
[string.byte('N')] = "n",
[string.byte('M')] = "m",
[string.byte('0')] = "0",
[string.byte('1')] = "1",
[string.byte('2')] = "2",
[string.byte('3')] = "3",
[string.byte('4')] = "4",
[string.byte('5')] = "5",
[string.byte('6')] = "6",
[string.byte('7')] = "7",
[string.byte('8')] = "8",
[string.byte('9')] = "9",
--[255] = "printscreen" --these keys do not work
}
-- define callback for whole device
lmc_set_handler('MACROS', function(button, direction)
--Ignoring upstrokes ensures keystrokes are not registered twice, but activates faster than ignoring downstrokes. It also allows press and hold behaviour
if (direction == 0) then return end -- ignore key upstrokes.
if type(config[button]) == "string" then
print(' ')
print('Your key ID number is: ' .. button)
print('It was assigned string: ' .. config[button])
sendToAHK(config[button])
else
print(' ')
print('Not yet assigned: ' .. button)
end
end)

There's a comma missing after the string here:
[108] = "numEnter" --sometimes this is different, check your keyboard

Related

GSUB doesnt works without spaces

I was trying to make a simple text cryptor, but the script works only if put spaces after every symbol
code:
local text = ""
local tdext = text:gsub("%S+", {["+"] = "a", ["×"] = "b", ["÷"] = "c", ["="] = "d", ["/"] = "e", ["_"] = "f", ["€"] = "g", ["¥"] = "h", ["₩"] = "i", ["!"] = "j", ["#"] = "k", ["#"] = "l", ["$"] = "m", ["%"] = "n", ["^"] = "o", ["&"] = "p", ["*"] = "q", ["("] = "r", [")"] = "s", ["-"] = "t", ["'"] = "u", [":"] = "v", [";"] = "w", [","] = "x", ["?"] = "y", ["."] = "z", [" "] = " "})
print(tdext)
I tried fixing it, but it doesnt do what it should.
If i put in text variable "÷ =" it outputs "b c", but if i am putting "÷=" in variable it will output "÷=".
Let's take a closer look at your substitutions:
local subs = {
["+"] = "a", ["×"] = "b", ["÷"] = "c", ["="] = "d", ["/"] = "e",
["_"] = "f", ["€"] = "g", ["¥"] = "h", ["₩"] = "i", ["!"] = "j",
["#"] = "k", ["#"] = "l", ["$"] = "m", ["%"] = "n", ["^"] = "o",
["&"] = "p", ["*"] = "q", ["("] = "r", [")"] = "s", ["-"] = "t",
["'"] = "u", [":"] = "v", [";"] = "w", [","] = "x", ["?"] = "y",
["."] = "z", [" "] = " "
}
local tdext = text:gsub("%S+", subs)
%S+ matches a sequence of one or more non-space bytes. If you have single characters - multi-byte (UTF-8) or single-byte (ASCII) - this will work fine. However if you have a sequence of multiple characters (say, +-), this won't perform the replacement, since both + and - won't be found in your lookup table. The same is the case for the multi-byte ÷=: ÷ = works, because your characters are separated by spaces; ÷= doesn't, because the pattern greedily matches the sequence.
If this is supposed to be a character-wise substitution, you'll need to match characters (UTF-8 sequences, which includes ASCII). Lua 5.3 and later will have the "constant" utf8.charpattern which is a pattern string matching a single UTF-8 character. If you have a recent Lua version, the fix becomes trivial: Just replace "%S+" with utf8.charpattern:
local tdext = text:gsub(utf8.charpattern, subs)
In older Lua versions (up to and including 5.2), you'll have to write this pattern yourself, using decimal escapes:
local charpattern = "[%z-\127\194-\244][\128-\191]*"
local tdext = text:gsub(charpattern, subs)
Alternatively, if you also want to support multi-character substitutions, you can simply apply the substitutions one by one (which is however significantly less efficient by a factor linear in the number of entries in the subs table):
-- We need to escape everything to make Lua treat it as a literal string
local function escape_pattern(str)
return str:gsub(".", "%%.")
end
local tdext = text
for from, to in pairs(subs) do
tdext = tdext:gsub(escape_pattern(from), escape_pattern(to))
end

How do I translate substrings in Lua?

I want to make a translator that translates R U R' U' into r^ u< rv u>. This is very difficult because R' has R in it. So when I try to use this, it spits a result back to me like
put in your algorithm: R U R' U'
r^ u< r^' u<'
Not a very human way of doing it. I think this is because R is a substring of R', but this is what I have been told. I am using this translation thingy from a package called luastring. The dev of luastring won't help me, and this question is too advanced for everyone on the discord server. This is what I have tried so far.
io.write("put in your algorithm: ")
local alg = io.read()
function split(str, pat)
local t = {}
local fpat = "(.-)" .. pat
local last_end = 1
local s, e, cap = str:find(fpat, 1)
while s do
if s ~= 1 or cap ~= "" then table.insert(t, cap) end
last_end = e + 1
s, e, cap = str:find(fpat, last_end)
end
if last_end <= #str then
cap = str:sub(last_end)
table.insert(t, cap)
end
return t
end
-- table stuff so we can see
local moves = {"R", "U", "L", "F", "D", "B", "M", "E", "S", "R'", "U'", "L'", "F'", "D'", "B'", "M'", "E'", "S'"}
local neomoves = {"r^", "u<", "lv", "f>", "d>", "b<", "mv", "e>", "s>", "rv", "u>", "l^", "f<", "d<", "b>", "m^", "e<", "s<"}
local string = require("luastring")
local translation_table = {
["R'"] = "rv", ["U'"] = "u<", ["L'"] = "lv", ["F'"] = "f>", ["D'"] = "d>", ["B'"] = "b<", ["M'"] = "mv", ["E'"] = "e>", ["S'"] = "s>", ["R"] = "r^", ["U"] = "u<", ["L"] = "lv", ["F"] = "f>", ["D"] = "d>", ["B"] = "b<", ["M"] = "mv", ["E"] = "e>", ["S"] = "s>"
}
-- translate the moves to neomoves
local translated = string.translate(alg, translation_table)
print(translated)
What on earth do I have to do to let Lua know what it is supposed to do?
As your translated string is lower case and the source string is upper case you can simply translate R' befor you translate R.
You only need to split your translation_table into two.
Also you can simply use Lua's standard string.gsub for this. I don't see why you would use luastring here.
Simplified example:
local str = "R' R R' R'"
local t_a = {["R'"] = "rv"}
local t_b = {["R"] = "r^"}
print((str:gsub("%S+", t_a):gsub("%S+", t_b)))

Changing value in table in Lua

I am trying to make a table and want to change a value in that table for a particular key. The thing is when I do change the key it does change for all the keys.
function dump(o, nb)
if nb == nil then
nb = 0
end
if type(o) == 'table' then
local s = ''
for i = 1, nb + 1, 1 do
s = s .. " "
end
s = '{\n'
for k,v in pairs(o) do
if type(k) ~= 'number' then k = '"'..k..'"' end
for i = 1, nb, 1 do
s = s .. " "
end
s = s .. '['..k..'] = ' .. dump(v, nb + 1) .. ',\n'
end
for i = 1, nb, 1 do
s = s .. " "
end
return s .. '}'
else
return tostring(o)
end
end
Config={}
PlayersStatusTable={}
Config.DefaultStatus = {
hunger = 1000000,
thirst = 1000000,
}
local timeNow = os.clock()
PlayersStatusTable[12] = Config.DefaultStatus
PlayersStatusTable[112] = Config.DefaultStatus
PlayersStatusTable[54] = Config.DefaultStatus
for playerId, details in pairs(PlayersStatusTable) do
print("playerid1",playerId)
print(dump(PlayersStatusTable))
print(dump(PlayersStatusTable[112]))
print(dump(PlayersStatusTable[112].hunger))
PlayersStatusTable[112].hunger = 5
end
the output is this:
playerid1 112
{
[112] = {
["thirst"] = 1000000,
["hunger"] = 1000000,
},
[54] = {
["thirst"] = 1000000,
["hunger"] = 1000000,
},
[12] = {
["thirst"] = 1000000,
["hunger"] = 1000000,
},
}
{
["thirst"] = 1000000,
["hunger"] = 1000000,
}
1000000
playerid1 54
{
[112] = {
["thirst"] = 1000000,
["hunger"] = 5,
},
[54] = {
["thirst"] = 1000000,
["hunger"] = 5,
},
[12] = {
["thirst"] = 1000000,
["hunger"] = 5,
},
}
{
["thirst"] = 1000000,
["hunger"] = 5,
}
5
playerid1 12
{
[112] = {
["thirst"] = 1000000,
["hunger"] = 5,
},
[54] = {
["thirst"] = 1000000,
["hunger"] = 5,
},
[12] = {
["thirst"] = 1000000,
["hunger"] = 5,
},
}
{
["thirst"] = 1000000,
["hunger"] = 5,
}
5
I just want the hunger of id 112 to be 5.
You're assigning the same table to all 3 keys, so they all point to the same table that's being changed. You need to ensure that you're creating a new table when you assign to each key.
local function shallowCopy(t)
local result = {}
for k, v in pairs(t) do
result[k] = v
end
return result
end
PlayersStatusTable[12] = shallowCopy(Config.DefaultStatus)

How to cope with straddled fields in Wireshark lua dissector?

I'm writing a Wireshark Lua dissector for a protocol that straddles fields across octet boundaries:
Octet 0:
bits 0..3: a
bits 4..6: b
bits 7: c
Octet 1:
bits 0..3: x
bits 4..7: y (ls nibble)
Octet 2:
bits 0..3: y (ms nibble)
bits 4..7: z
How would one manage these fields in Lua?
This should get you most of the way there. (The problem is with y since you indicated that the least significant nibble is in the lower octet rather than the most significant nibble as one might normally expect.)
local p_foo = Proto("foo", "FOO Protocol")
local f_foo_a = ProtoField.uint8("foo.a", "A", base.DEC, nil, 0xf0)
local f_foo_b = ProtoField.uint8("foo.b", "B", base.DEC, nil, 0x0e)
local f_foo_c = ProtoField.uint8("foo.c", "C", base.DEC, nil, 0x01)
local f_foo_x = ProtoField.uint8("foo.x", "X", base.DEC, nil, 0xf0)
local f_foo_y = ProtoField.uint16("foo.y", "Y", base.DEC, nil, 0x0ff0)
local f_foo_z = ProtoField.uint8("foo.z", "Z", base.DEC, nil, 0x0f)
p_foo.fields = { f_foo_a, f_foo_b, f_foo_c, f_foo_x, f_foo_y, f_foo_z }
function p_foo.dissector(buf, pinfo, tree)
local foo_tree = tree:add(p_foo, buf(0,-1))
pinfo.cols.protocol:set("FOO")
foo_tree:add(f_foo_a, buf(0, 1))
foo_tree:add(f_foo_b, buf(0, 1))
foo_tree:add(f_foo_c, buf(0, 1))
foo_tree:add(f_foo_x, buf(1, 1))
foo_tree:add(f_foo_y, buf(1, 2))
foo_tree:add(f_foo_z, buf(2, 1))
end
-- Registration: TODO
If you really need to handle y as you indicated, then you'll have to bit-swap. There's probably a more elegant way to do this, but here's one solution:
local p_foo = Proto("foo", "FOO Protocol")
local f_foo_a = ProtoField.uint8("foo.a", "A", base.DEC, nil, 0xf0)
local f_foo_b = ProtoField.uint8("foo.b", "B", base.DEC, nil, 0x0e)
local f_foo_c = ProtoField.uint8("foo.c", "C", base.DEC, nil, 0x01)
local f_foo_x = ProtoField.uint8("foo.x", "X", base.DEC, nil, 0xf0)
local f_foo_y = ProtoField.uint16("foo.y", "Y", base.DEC, nil, 0x0ff0)
local f_foo_z = ProtoField.uint8("foo.z", "Z", base.DEC, nil, 0x0f)
p_foo.fields = { f_foo_a, f_foo_b, f_foo_c, f_foo_x, f_foo_y, f_foo_z }
nib2bin = {
[0] = "0000", [1] = "0001",
[2] = "0010", [3] = "0011",
[4] = "0100", [5] = "0101",
[6] = "0110", [7] = "0111",
[8] = "1000", [9] = "1001",
[10] = "1010", [11] = "1011",
[12] = "1100", [13] = "1101",
[14] = "1110", [15] = "1111"
}
function nibble2binary(n)
return nib2bin[bit.band(n, 0x0f)]
end
function p_foo.dissector(buf, pinfo, tree)
local foo_tree = tree:add(p_foo, buf(0,-1))
local y_lsn = bit.band(buf(1, 1):uint(), 0x0f)
local y_msn = bit.band(buf(2, 1):uint(), 0xf0)
local y = bit.bor(y_lsn, y_msn)
pinfo.cols.protocol:set("FOO")
foo_tree:add(f_foo_a, buf(0, 1))
foo_tree:add(f_foo_b, buf(0, 1))
foo_tree:add(f_foo_c, buf(0, 1))
foo_tree:add(f_foo_x, buf(1, 1))
foo_tree:add(f_foo_y, buf(1, 2)):set_text(".... " ..
nibble2binary(bit.rshift(y_msn, 4)) .. " " .. nibble2binary(y_lsn) ..
" .... = Y: " .. y)
foo_tree:add(f_foo_z, buf(2, 1))
end
-- Registration: TODO

mlr assertion of measures failed

I am running 4 classifiers on a dataset and comparing their performance. When I run the following code I receive some weird errors:
library(mlbench)
library(mlr)
maxx_IL10 = c(3199, 2997, 2690, 2482, 2891, 2310, 3180, 3050, 3115, 3052, 3071, 3068, 2611, 2723, 2903, 2969)
auc_INTERLEUKIN_12P70 = c(14809, 1384.5, 760, 10922.5, 3010, 14564, 26496, 1229, 2509, 1474.5, 20697.5, 1854.5, 17352, 1457, 227, 31507.5)
maxx_TNFA = c(3066, 2658, 2697, 3175, 2904, 2260, 2714, 3056, 3155, 3030, 3125, 2456, 3017, 2860, 1704, 3167)
fold_IL4 = c(16.02685, 0, 4.616438, 53.44898, 0, 0, 68.85714, 5.833333, 25.9, 0, 21.87629, 20.57895, 20.18792, 4.394737, 7.723404, 56.6383)
maxx_CD19 = c(3045.5, 3045.5, 3045.5, 3045.5, 2667, 1865, 3126, 2432, 3244, 3218, 2415, 3077, 3223, 2549, 3016, 3244)
auc_IL4 = c(18315.5, 0, 1348, 31112, 0, 0, 19182.5, 525, 3201.5, 0, 12976, 782, 19195.5, 835, 544.5, 26658)
Class = c("B", "A", "A", "A", "A", "A", "B", "A", "B", "B", "B", "A", "B", "A", "A", "B")
df = data.frame(maxx_IL10, auc_INTERLEUKIN_12P70, maxx_TNFA, fold_IL4, maxx_CD19, auc_IL4, Class)
Class.task = makeClassifTask( data = df, target = "Class", positive ="B")
fv = generateFilterValuesData(Class.task, method = "mrmr")
plotFilterValues(fv)
filtered.task = filterFeatures(Class.task, fval = fv, threshold = -.2)
n = getTaskSize(filtered.task)
train.set = sample(n, size = round(2/3 * n))
test.set = setdiff(seq_len(n), train.set)
lrn1 = makeLearner("classif.lda", predict.type = "prob")
mod1 = train(lrn1, filtered.task, subset = train.set)
pred1 = predict(mod1, task = filtered.task, subset = test.set)
lrn2 = makeLearner("classif.ksvm", predict.type = "prob")
mod2 = train(lrn2, filtered.task, subset = train.set)
pred2 = predict(mod2, task = filtered.task, subset = test.set)
lrn3 = makeLearner("classif.randomForest", predict.type = "prob")
mod3 = train(lrn3, Class.task, subset = train.set)
pred3 = predict(mod3, task = Class.task, subset = test.set)
lrn5 = makeLearner("classif.xgboost", predict.type = "prob")
mod5 = train(lrn5, Class.task, subset = train.set)
pred5 = predict(mod5, task = Class.task, subset = test.set)
### Tune wrapper for ksvm
rdesc.inner = makeResampleDesc("Holdout")
ms = list(auc, mmce)
ps = makeParamSet(
makeDiscreteParam("C", 2^(-1:1))
)
ctrl = makeTuneControlGrid()
lrn2 = makeTuneWrapper(lrn2, rdesc.inner,ms, ps, ctrl, show.info = FALSE)
lrns = list(lrn1, lrn2,lrn3,lrn5)
rdesc.outer = makeResampleDesc("CV", iters = 5)
bmr = benchmark(lrns, tasks = filtered.task, resampling = rdesc.outer, measures = ms, show.info = FALSE)
bmr
The errors that I receive are:
lrn2 = makeTuneWrapper(lrn2, rdesc.inner,ms, ps, ctrl, show.info = FALSE)
Error in makeTuneWrapper(lrn2, rdesc.inner, ms, ps, ctrl, show.info = FALSE) :
Assertion on 'measures' failed: May only contain the following types: Measure.
lrns = list(lrn1, lrn2,lrn3,lrn5)
rdesc.outer = makeResampleDesc("CV", iters = 5)
bmr = benchmark(lrns, tasks = filtered.task, resampling = rdesc.outer, measures = ms, show.info = FALSE)
Error in FUN(X[[i]], ...) :
List measures has element of wrong type function at position 1. Should be: Measure
bmr
Error: object 'bmr' not found
Any ideas on what I am doing wrong? Thank you!!

Resources