Spliting every 6 table items at a new table

image
I need to make a code what is gonna take every 6 items in table, and then split it like this:
image
Any ideas?

This is what CGPT has to say about it:

function splitArray(items, chunkSize)
	local result = {}
	for i = 1, #items, chunkSize do
		local chunk = {}
		for j = i, math.min(i + chunkSize - 1, #items) do
			table.insert(chunk, items[j])
		end
		table.insert(result, chunk)
	end
	return result
end

-- Example usage
local items = {"apple", "banana", "cherry", "date", "fig", "grape", "kiwi", "lemon", "mango", "nectarine"}
local splitItems = splitArray(items, 6)

print(splitItems)
1 Like

I made something just for this a while back I will find it

local function splitByNth(input : {any}, nth : number) : {any}
	local floored = math.floor(nth)
	local res = {}
	
	for i = 1, #input, floored do
		table.insert(res, (function() 
			local res = {}
			for x = i, (i + floored) - 1, 1 do
				table.insert(res, input[x])
			end
			return res
		end)())
	end
	
	return res
end

amazing, this helped, thank you for your help, have a good day/night!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.