How to make an unpacked table into a packed table?

When doing something like this:

local tabbytabley_cat = {'Me', 'is', 'cat'}
local letsgo = table.unpack(tabbytabley_cat)

local gobackto_urcage = string.split(letsgo, ' ')
print(gobackto_urcage)

It only prints one, which is: “Me”
How do I actually make an unpacked table into a packed table? (Like reversed table.unpack)

1 Like

To reverse the effect of table.unpack, you can use the table.pack function to create a new table with the unpacked values.

2 Likes

I noticed this solution at first, but then it just errors. The params of table.pack is a Tuple

1 Like

Have you tried an example I was making here

local tabbytabley_cat = {‘Me’, ‘is’, ‘cat’}
local letsgo = table.unpack(tabbytabley_cat)

local gobackto_urcage = table.pack(string.split(letsgo, ’ '))
print(gobackto_urcage)

?

2 Likes

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
1 Like

Are you sure? It still looks like you don’t understand the params of table.pack. It’s a tuple. This just puts it into {{‘Me’}, n = 1}

1 Like

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.

1 Like

Ah yes, I’ve read it wrongly, apologies. As @Y_VRN said you can use a loop to do it.

1 Like

You forgot ipairs() but alr

1 Like

Roblox recently added a feature where you can slap in a table without using ipairs or pairs. Vanilla Lua still needs those functions, but not Luau.

I still don’t think I can live with that but now i understand

1 Like

this for loop uses a table which i usually wouldnt have when it comes to packing a table

I’m not sure why would you need to unpack a table. Do you need to modify its contents?

1 Like

yeah, its for deobfuscating a puzzle


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 code isn’t a long string but rather a set of strings.

You have two options:

  1. Remove table.unpack from the return value of code
  2. 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
1 Like

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

bro i cant believe the … trick that i didnt notice just made this topic even longer this actually fixed my table.pack problem

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?

thanks for trying but i probably didnt provide much detail. read post 16