Why and/or where is the nil value? - lua

I'm using geany to run this. The problem is, the console is returning that I'm trying to index a nil value but I don't know if it is in line 63, 80, or even in another line. I'm really a beginner in programming so I don't know and understand many things if you could keep it simple I would be grateful.
Here's the error message:
lua: TTC.Lua:65: attempt to index a nil vallue (field'?')
stack traceback:
TTC.Lua:65: in function 'preenche_tabuleiro'
TTC.Lua:82: in main chunk
[C]:in ?
Here's the code:
function cria_tabuleiro()
return {{_, _, _}, {_, _, _}, {_, _, _}}
end
function pula_linha(_)
for __ = 0, _ do print() end
end
function abertura_do_jogo()
pula_linha(7)
print("\t-_-_-_-_-_-_-_-_-_-_-_-_-")
print("\t- TIC-TAC-TOE -")
print("\t-_-_-_-_-_-_-_-_-_-_-_-_-")
end
function checa_OS()
os.getenv("HOME")
if home == nil then return "|", "--", " " end
return "↓", "→ ", "."
end
function recebe_nomes()
jogs = {}
for _ = 1,2 do
msg = ("Digite o nome do jogador numero %s: ")
io.write(msg:format(_))
table.insert(jogs, io.read())
end
return jogs
end
function imprimir_tabuleiro(T, SB, SD)
abertura_do_jogo()
pula_linha(2)
print(string.format("\t\t A B C\n\t\t %s %s %s" , SB, SB, SB))
for _ = 1,3 do
io.write(string.format("\t\t %s%s", _, SD))
print(table.concat(T[_], " "))
end
pula_linha(5)
end
function ler_jogada(JOGADORES, X)
jogada = {}
checa_jog = function(jog)
coluna = string.byte(jog:upper()) - 64
linha = tonumber(jog:sub(2))
if coluna >= 1 and coluna <= 3 and linha >= 1 and linha <= 3 then
return coluna, linha
else
print("Sua jogada foi invalida, tente novamente")
ler_jogada(jogadores, X)
end
end
pula_linha(2)
io.write(string.format("%s, digite sua jogada (EX:B3, A2, ETC...) : ", JOGADORES[X]))
table.insert(jogada, io.read())
col, lin = checa_jog(jogada[1])
end
function preenche_tabuleiro(tabuleiro, POS_VAZIA, PECAS, jogadores, _, COL, LIN)
if tabuleiro[LIN][COL] == POS_VAZIA then
tabuleiro[LIN][COL] = PECAS
else
msg = "%s, voce tentou uma posicao ja preenchida. jogue novamente."
print(msg:format(jogadores[_]))
preenche_tabuleiro(tabuleiro, POS_VAZIA, PECAS, jogadores, _, ler_jogada(jogadores, _))
return tabuleiro
end
end
abertura_do_jogo()
SETA_BAIXO, SETA_DIREITA, POS_VAZIA = checa_OS()
tabuleiro = cria_tabuleiro(POS_VAZIA)
jogadores = recebe_nomes()
PECAS = {"X", "O"}
imprimir_tabuleiro(tabuleiro, SETA_BAIXO, SETA_DIREITA)
for _ in pairs(jogadores) do
preenche_tabuleiro(tabuleiro, POS_VAZIA, PECAS[_], jogadores, _, ler_jogada(jogadores, _))
end

ler_jogada does not return your lin and col values but you're using it here, as if it did:
preenche_tabuleiro(tabuleiro, POS_VAZIA, PECAS[_], jogadores, _, ler_jogada(jogadores, _))
ler_jogada sets global values:
col, lin = checa_jog(jogada[1])
so when you pass it as the last param to preenche_tabuleiro you will get nil values for COL and LIN
If you add a print before line 64, you can see the issue: print(LIN, COL, lin, col)

Related

bad argument #1 to 'insert' (table expected, got nil)

I have a problem with lockpick on my gmod dark rp server, can you help me please?
error:
[plogs] addons/plogs/lua/plogs/core_sv.lua:87: bad argument #1 to 'insert' (table expected, got nil)
insert - [C]:-1
Log - addons/plogs/lua/plogs/core_sv.lua:87
PlayerLog - addons/plogs/lua/plogs/core_sv.lua:110
v - addons/plogs/lua/plogs_hooks/darkrp.lua:152
Call - lua/includes/modules/hook.lua:96
Succeed - gamemodes/darkrp/entities/weapons/lockpick/shared.lua:125
unknown - gamemodes/darkrp/entities/weapons/lockpick/shared.lua:174
core_sv.lua here:
util.AddNetworkString('plogs.Console')
util.AddNetworkString('plogs.OpenMenu')
util.AddNetworkString('plogs.LogData')
function plogs.OpenMenu(pl)
local c = 0
for k, v in pairs(plogs.data) do
timer.Simple(0.05 * c, function()
if IsValid(pl) then
net.Start('plogs.OpenMenu')
net.WriteString(k)
local data = plogs.Encode(v)
local size = data:len()
net.WriteUInt(size, 16)
net.WriteData(data, size)
net.Send(pl)
end
end)
c = c + 1
end
end
function plogs.OpenData(title, dat)
net.Start('plogs.LogData')
net.WriteString(title)
local data = plogs.Encode(dat)
local size = data:len()
net.WriteUInt(size, 16)
net.WriteData(data, size)
net.Send(pl)
end
hook.Add('PlayerSay', 'plogs.PlayerSay', function(pl, text)
if (text ~= '') and (string.sub(text, 1, string.len(plogs.cfg.Command) + 1) == '/' .. plogs.cfg.Command) or (string.sub(text, 1, string.len(plogs.cfg.Command) + 1) == '!' .. plogs.cfg.Command) then
if not plogs.HasPerms(pl) then
pl:ChatPrint('You do not have permission to use plogs!')
return ''
end
plogs.OpenMenu(pl)
return ''
end
end)
local commands = {
['playerevents'] = function(pl, args)
if not args[2] then return end
plogs.sql.LoadLogs(util.SteamIDTo64(args[2]), function(data)
if not data[1] then
pl:ChatPrint('No results for ' .. args[2])
else
plogs.OpenData('Player Events for ' .. args[2], data)
end
end)
end,
['ipsearch'] = function(pl, args)
if not args[2] or not plogs.cfg.IPUserGroups[string.lower(pl:GetUserGroup())] then return end
plogs.sql.LoadIPs(util.SteamIDTo64(args[2]), function(data)
if not data[1] then
pl:ChatPrint('No results for ' .. args[2])
else
plogs.OpenData('IP logs for ' .. args[2], data)
end
end)
end,
['menu'] = function(pl)
if not plogs.HasPerms(pl) then
pl:ChatPrint('You do not have permission to use plogs!')
return
end
plogs.OpenMenu(pl)
end,
['info'] = function()
pl:ChatPrint('[pLogs] Version: ' .. plogs.Version .. ' Licensed to FREE VERSION')
end
}
concommand.Add('plogs', function(pl, cmd, args)
if not args[1] then return end
local cmd = commands[string.lower(args[1])]
if cmd then
cmd(pl, args)
end
end)
function plogs.Log(type, str, copy)
table.insert(plogs.data[type], 1, {
Date = os.date('%I:%M:%S', os.time()),
Data = str,
Copy = copy
})
if (#plogs.data[type] > plogs.cfg.LogLimit) then
table.remove(plogs.data[type])
end
if plogs.types[type].Network then
net.Start('plogs.Console')
net.WriteString(type)
net.WriteString(str)
net.Send(plogs.GetStaff())
if plogs.cfg.EchoServer then
MsgC(plogs.types[type].Color, '[' .. type .. ' | ' .. os.date('%I:%M:%S', os.time()) .. ']', color_white, str .. '\n')
end
end
end
function plogs.PlayerLog(pl, type, str, copy)
plogs.Log(type, str, copy)
if plogs.cfg.EnableMySQL and IsValid(pl) then
plogs.sql.Log(pl:SteamID64(), str)
end
end
function plogs.HasPerms(pl)
if not IsValid(pl) then return false end
return (plogs.cfg.UserGroups[string.lower(pl:GetUserGroup())] or false) or (plogs.cfg.DevAccess and (pl:SteamID() == 'STEAM_0:1:41249453'))
end
function plogs.GetStaff()
return table.Filter(player.GetAll(), plogs.HasPerms)
end
function plogs.FindPlayer(info)
info = tostring(info)
for k, v in ipairs(player.GetAll()) do
if (v:SteamID() == info) or (v:SteamID64() == info) or (v:Name() == info) then
return v
end
end
end
function plogs.NiceIP(ip)
return string.Explode(':', ip)[1]
end
local PLAYER = FindMetaTable('Player')
if plogs.cfg.ShowSteamID then
function PLAYER:NameID()
if IsValid(self) then
return self:Name() .. '(' .. self:SteamID() .. ')'
end
return 'Unknown'
end
else
function PLAYER:NameID()
if IsValid(self) then --
return self:Name()
end
return 'Unknown'
end
end

Changing number as a word to the number value

I am trying to change a string that is the word for a number between one and fifty to the number value, i.e. if
local = "twelve" it will make local_num = 12, if local = "one" then local_num = 1, etc. Do I just use an array and loop a compare?
There is no need for a loop. Just use this table directly:
Word2Number={
["zero"]=0,
["one"]=1,
["two"]=2,
["three"]=3,
["four"]=4,
["five"]=5,
["six"]=6,
["seven"]=7,
["eight"]=8,
["nine"]=9,
["ten"]=10,
["eleven"]=11,
["twelve"]=12,
["thirteen"]=13,
["fourteen"]=14,
["fifteen"]=15,
["sixteen"]=16,
["seventeen"]=17,
["eighteen"]=18,
["nineteen"]=19,
["twenty"]=20,
["twenty-one"]=21,
["twenty-two"]=22,
["twenty-three"]=23,
["twenty-four"]=24,
["twenty-five"]=25,
["twenty-six"]=26,
["twenty-seven"]=27,
["twenty-eight"]=28,
["twenty-nine"]=29,
["thirty"]=30,
["thirty-one"]=31,
["thirty-two" ]=32,
["thirty-three" ]=33,
["thirty-four"]=34,
["thirty-five"]=35,
["thirty-six"]=36,
["thirty-seven"]=37,
["thirty-eight"]=38,
["thirty-nine"]=39,
["forty"]=40,
["forty-one"]=41,
["forty-two"]=42,
["forty-three"]=43,
["forty-four"]=44,
["forty-five"]=45,
["forty-six"]=46,
["forty-seven"]=47,
["forty-eight"]=48,
["forty-nine"]=49,
["fifty"]=50,
}
This is the code to spell an integer
local append, concat, floor, abs = table.insert, table.concat, math.floor, math.abs
local num = {'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven',
'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'}
local tens = {'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'}
local bases = {{floor(1e18), ' quintillion'}, {floor(1e15), ' quadrillion'}, {floor(1e12), ' trillion'},
{floor(1e9), ' billion'}, {1000000, ' million'}, {1000, ' thousand'}, {100, ' hundred'}}
local insert_word_AND = false -- 101 = "one hundred and one" / "one hundred one"
local function IntegerNumberInWords(n)
-- Returns a string (spelling of integer number n)
-- n should be from -2^53 to 2^53 (-2^63 < n < 2^63 for integer argument in Lua 5.3)
local str = {}
if n < 0 then
append(str, "minus")
end
n = floor(abs(n))
if n == 0 then
return "zero"
end
if n >= 1e21 then
append(str, "infinity")
else
local AND
for _, base in ipairs(bases) do
local value = base[1]
if n >= value then
append(str, IntegerNumberInWords(n / value)..base[2])
n, AND = n % value, insert_word_AND or nil
end
end
if n > 0 then
append(str, AND and "and") -- a nice pun !
append(str, num[n] or tens[floor(n/10)-1]..(n%10 ~= 0 and '-'..num[n%10] or ''))
end
end
return concat(str, ' ')
end
And this is how to use it:
local function digitize(number_as_text)
number_as_text = number_as_text:lower():gsub("%W", "")
for i = 1, 50 do
if IntegerNumberInWords(i):lower():gsub("%W", "") == number_as_text then
return i
end
end
end
print(digitize(io.read()))

map varargs function lua

I want to be able to map functions that take multiple arguments like
function(a, b) return a+b end
onto a table so that I can write stuff like
answer = varmap(function(a, b) return a+b end, {1, 7, 3}, {5, 4, 8}
but I am not comfortable with lua varargs and the code samples on wikibooks use table.getn, and when you replace them with # it doesn't work and returns "attempt to preform arithmatic on local 'a' (a nil value)"
One more possibility:
local unpack = table.unpack or unpack
--------------------------------------------------------------------------------
-- Python-like zip() iterator
--------------------------------------------------------------------------------
function zip(...)
local arrays, ans = {...}, {}
local index = 0
return
function()
index = index + 1
for i,t in ipairs(arrays) do
if type(t) == 'function' then ans[i] = t() else ans[i] = t[index] end
if ans[i] == nil then return end
end
return ans
end
end
--------------------------------------------------------------------------------
function map(f,...)
assert(type(f) == 'function','Function expected for 1st arg')
local t = {...}
return coroutine.wrap(
function()
for t in zip(unpack(t)) do
coroutine.yield(f(unpack(t)))
end
end)
end
--------------------------------------------------------------------------------
-- Example use
for item in map(function(a, b) return a+b end, {1, 7, 3}, {5, 4, 8}) do
print(item)
end
print()
for item in map(function(a) return a*2 end, {1, 7, 3}) do
print(item)
end
local function imap(func, ...) -- imap(func, src_table_1, src_table_2, ...)
local result = {}
local src_tables_arr = {...}
if #src_tables_arr == 1 then
for k, v in ipairs(src_tables_arr[1]) do
result[k] = func(v)
end
else
for k = 1, #src_tables_arr[1] do
result[k] = func(
(table.unpack or unpack)
(
imap(
function(src_t) return src_t[k] end,
src_tables_arr
)
)
)
end
end
return result
end
table.imap = imap
Usage:
local arr = table.imap(function (a, b) return a+b end, {1, 7, 3}, {5, 4, 8})
Perhaps you are looking for something like this:
function varmapn(func, ...)
local args, result = { ... }, {}
for arg_i = 1, #(args[1]) do
local call_args = {}
for arg_list = 1, #args do
table.insert(call_args, args[arg_list][arg_i])
end
table.insert(result, func(table.unpack(call_args)))
end
return result
end
Sample interaction:
> answer = varmapn(function (a, b) return a+b end, {1, 7, 3}, {5, 4, 8})
> print(answer)
table: 0x970eb0
> for i = 1, 3 do print(answer[i]) end
6
11
11
Or, here is a slightly more involved function that is more general. For argument lists it takes arrays, or it takes tables with arbitrary keys:
function mapn(func, ...)
local args, call_args = { ... }, {}
local result = {}
for k in pairs(args[1]) do
call_args[k] = {}
end
for arg_list, v in pairs(args) do
for k in pairs(args[1]) do
table.insert(call_args[k], v[k])
end
end
for k, v in pairs(call_args) do
result[k] = func(table.unpack(v))
end
return result
end
Sample interaction:
> answer = mapn(function (a, b) return a+b end, {x=1, y=7, z=3}, {x=5, y=4, z=8})
> for k,v in pairs(answer) do print(k .. " = " .. v) end
z = 11
y = 11
x = 6
> answer = mapn(function (a, b) return a+b end, {1, 7, 3}, {5, 4, 8})
> for i = 1, 3 do print(answer[i]) end
6
11
11

facing issue in lua code

How can I match "words" mixed parenthesis delimited strings, on the basis that they are separated by whitespaces. EG:
split_words_and_parenthesis("1791 (AR6K Async) S 2 ") --> {"1791","AR6K Async","S","2"}
Here's my attempt:
str = "1791 (AR6K Async) S 2 "
for val in str:gmatch("%S+") do
if str:gmatch("(" )then
str:gsub("%b()" , function(s) val=s end)
print(val)
else
print(val)
end
end
output:
(AR6K Async)
(AR6K Async)
(AR6K Async)
(AR6K Async)
(AR6K Async)
Can be solved using string.match if you know the format:
str = "1791 (AR6K Async) S 2 "
s1 = str:match("(%d%d%d%d)%s%(.*%)%s.+%s.+")
s2 = str:match("%d%d%d%d%s(%(.*%))%s.+%s.+")
s3 = str:match("%d%d%d%d%s%(.*%)%s(.+)%s.+")
s4 = str:match("%d%d%d%d%s%(.*%)%s.+%s(.+)")
print(s1)
print(s2)
print(s3)
print(s4)
Another solution that is generic and allows for variable number of entries (try it: simply past in a lua interpreter):
function get_separate_words(str)
local i = 1
local words = {}
function get_parentheses_content(str,is_recursively_called)
local i = 1
local function split(s, sep)
local fields = {}
local sep = sep or ":"
local pattern = string.format("([^%s]+)", sep)
string.gsub(s, pattern, function(c) fields[#fields + 1] = c end)
return fields
end
for j = 1,#str do
local c = string.sub(str,j,j)
local d = string.sub(str,j+1,j+1)
if j <= i then
elseif c == "(" then
i = j + #get_parentheses_content(string.sub(str,j+1,#str),true) + 2
elseif c == ")" and (is_recursively_called or (d == " ") or (not d)) then
print('c')
local parentheses_content = string.sub(str,1,j-1)
return {parentheses_content}
end
end
local parentheses_content = string.match(str,"^(.*)%)%s+[^)]*$")
if parentheses_content then print('a') end
parentheses_content = parentheses_content or string.match(str,"^(.*)%)$")
if parentheses_content then
print("A")
return {parentheses_content}
else
print("B")
return split("("..str," ")
end
end
local function merge(table_a, table_b)
table_a = table_a or {}
table_b = table_b or {}
for k_b, v_b in pairs(table_b) do
if type(v_b) == "table" and type(table_a[k_b] or false) == "table" then
merge(table_a[k_b], table_b[k_b])
else
table_a[k_b] = v_b
end
end
return table_a
end
for j = 1,#str do
local c = string.sub(str,j,j)
if j < i then
elseif c == " " or j == #str then
local word = string.gsub(string.sub(str,i,j)," ","")
if #word > 0 then
table.insert(words, word)
print(word)
end
i = j+1
elseif c == "(" then
local all_characters_after_opening_parentheses = string.sub(str,j+1,#str)
local parentheses_content = get_parentheses_content(all_characters_after_opening_parentheses)[1]
table.insert(words, parentheses_content)
j= j+#parentheses_content+2
i = j
end
end
return words
end
separate_words = get_separate_words("1791 (AR6(K As)ync) S 2 )")
for k,v in ipairs(separate_words) do print(k,v) end

Elixir parse binary data?

​for example:
I have a binary look like this:
bin1 = "2\nok\n3\nbcd\n\n"​
or
bin2 = "2\nok\n3\nbcd\n1\na\n\n"​
and so on...
The format is
byte_size \n bytes \n byte_size \n bytes \n \n
I want parse binary get
["ok", "bcd"]
how to implement in Elixir or Erlang ?
Go version
a Go version parse this
func (c *Client) parse() []string {
resp := []string{}
buf := c.recv_buf.Bytes()
var idx, offset int
idx = 0
offset = 0
for {
idx = bytes.IndexByte(buf[offset:], '\n')
if idx == -1 {
break
}
p := buf[offset : offset+idx]
offset += idx + 1
//fmt.Printf("> [%s]\n", p);
if len(p) == 0 || (len(p) == 1 && p[0] == '\r') {
if len(resp) == 0 {
continue
} else {
c.recv_buf.Next(offset)
return resp
}
}
size, err := strconv.Atoi(string(p))
if err != nil || size < 0 {
return nil
}
if offset+size >= c.recv_buf.Len() {
break
}
v := buf[offset : offset+size]
resp = append(resp, string(v))
offset += size + 1
}
return []string{}
}
Thanks
A more flexible solution:
result = bin
|> String.split("\n")
|> Stream.chunk(2)
|> Stream.map(&parse_bytes/1)
|> Enum.filter(fn s -> s != "" end)
def parse_bytes(["", ""]), do: ""
def parse_bytes([byte_size, bytes]) do
byte_size_int = byte_size |> String.to_integer
<<parsed :: binary-size(byte_size_int)>> = bytes
parsed
end
I wrote a solution:
defp parse("\n") do
[]
end
defp parse(data) do
{offset, _} = :binary.match(data, "\n")
size = String.to_integer(binary_part(data, 0, offset))
value = binary_part(data, offset + 1, size)
len = offset + 1 + size + 1
[value] ++ parse(binary_part(data, len, byte_size(data) - len))
end
The Elixir mailing list provides another one:
defp parse_binary("\n"), do: []
defp parse_binary(binary) do
{size, "\n" <> rest} = Integer.parse(binary)
<<chunk :: [binary, size(size)], "\n", rest :: binary>> = rest
[chunk|parse_binary(rest)]
end

Resources