Unpack expects you have multiple variables set up:
local a, b, c = table.unpack({1, 2, 3})
print(a, b, c) -- 1, 2, 3
Instead of using table.unpack, use a for loop:
local tabbytabley_cat = {'Me', 'is', 'cat'}
for _, letsgo in tabbytabley_cat do
local gobackto_urcage = string.split(letsgo, ' ')
print(gobackto_urcage)
end
Excuse me but your initial code doesn’t make much sense to me.
You are unpacking {'Me', 'is', 'cat'} into 'Me', 'is', 'cat', then storing only the first value (‘Me’) into letsgo and splitting it by spaces (which there are none) so the end result is 'Me'.
Sorry if I’m wrong but I think you want to concatenate the table with spaces like this:
local tabbytabley_cat = {'Me', 'is', 'cat'}
print(table.concat(tabbytabley_cat, " "))
Edit: I hope I’m not being rude. If I am, I apologize.
function code(str)
local nums = {}
for i = 1, str:len() do
table.insert(nums, string.format('%X', string.format('%o', string.byte(str:sub(i, i)))))
end
return table.unpack(nums)
end
function decode(str)
local result = ''
local tab = str:split(' ')
print(tab)
for i = 1, #tab do
result = result .. string.char(tonumber(tonumber(tab[i], 16), 8))
end
return result
end
print(decode(code('Hellooooooooooo')))
The first problem I see is that code returns a variadic argument.
The return value of codeisn’t a long string but rather a set of strings.
You have two options:
Remove table.unpack from the return value of code
Wrap code with table.pack:
print(decode(table.pack(code('Helloooooooo'))))
Then problem 2 is that decode expects a string, when it should expect a table instead.
function decode(tab)
local result = ''
print(tab)
for i = 1, #tab do
result = result .. string.char(tonumber(tonumber(tab[i], 16), 8))
end
return result
end
If you only want to modify decode, you can use this instead:
function decode(...)
local result = ''
local tab = table.pack(...)
print(tab)
for i = 1, #tab do
result = result .. string.char(tonumber(tonumber(tab[i], 16), 8))
end
return result
end
It appears that code is meant to return a string. There are two ways you can do this:
1. Just return a table (`code`) and read the table (`decode`):
function code(str)
local nums = {}
for i = 1, str:len() do
table.insert(nums, string.format('%X', string.format('%o', string.byte(str:sub(i, i)))))
end
return nums -- Return the table itself.
end
function decode(tab) -- Accepts a table.
local result = ''
print(tab)
for i = 1, #tab do
result = result .. string.char(tonumber(tonumber(tab[i], 16), 8))
end
return result
end
print(decode(code('Hellooooooooooo')))
2. Concatenate each code.
function code(str)
local encoded = ""
for i = 1, str:len() do
encoded = encoded .. string.format('%X', string.format('%o', string.byte(str:sub(i, i)))) .. " "
end
return encoded:sub(1, encoded:len() - 1) -- Send encoded string without the last space.
end
function decode(str)
local result = ''
local tab = str:split(' ')
print(tab)
for i = 1, #tab do
result = result .. string.char(tonumber(tonumber(tab[i], 16), 8))
end
return result
end
print(decode(code('Hellooooooooooo')))
table.unpack returns a tuple, meaning you need to have a variable on the left-hand side of the assignment to capture the tuple values.
local myTable = {1, 2, 3, 4}
local one, two, three, four = table.unpack(myTable)
print(one, two, three, four) -- 1 2 3 4
-- Repack the returned values.
local myTable2 = table.pack(one, two, three, four)
-- Iterating over myTable2 will not work as there's an extra 'n' key, but you could
-- use ipairs to avoid it.
for index, value in myTable do
print(value == myTable2[index]) -- true
end
If you were to attempt to assign to only 1 variable, you’d only get the first entry in the table (in the example above, it would have been 1).
If you want to concatenate the values of a table into a string, you can use table.concat.
local myTable = {"Hello,", "World!", "How", "are", "you", "doing", "today?"}
local message = table.concat(myTable, " ")
print(message) -- Hello, World! How are you doing today?