Table to Dictionary "Pages"?

I want to make a table to a dictionary with the index as a page number.
I have tried so many different methods I think my brain has fried.


I want to turn This:

local Table = {
	[1] = {},
	[2] = {},
	[3] = {},
	[4] = {},
	[5] = {},
	[6] = {}
}

Into This:

local Dictionary = {
[1] = {
	[1] = {},
	[2] = {},
	[3] = {},
},
​
[2] = {
	[1] = {},
	[2] = {},
	[3] = {},
}
}



This is the script I’ve been using: --// I got it to work but then my studio crashed…

ServerManager.GetPages = function(TableEntries, PageSize, Pages)
	
	if #TableEntries <= 0 then return {} end
	
	if PageSize == nil then PageSize = 100 end
	if Pages == nil then Pages = math.huge end
	
	local PageEntries = {}
	
	local CurrentPage = 0
	
	local EntriesNeeded = math.ceil(#TableEntries / PageSize)
	--print(EntriesNeeded)
	
	for i = 1, EntriesNeeded, 1 do
		
		local Start = 
		local End = #TableEntries * (i + 1)
		
		for j = Start, End, 1 do
			print(j)
		end 
		
		print(Start, End)
		
		--local NewEntry = {}
		--print(i, PageSize)
		
		
		
		--[[
		
		
		local Start = (CurrentPage - 1) * PageSize
		
		for j = Start, i, 1 do
			--print({i, CurrentPage, j}, CurrentPage * PageSize)
			table.insert(NewEntry, TableEntries[i])
		end
		
		print(NewEntry)
		
		if CurrentPage <= Pages and #NewEntry > 0 then
			table.insert(PageEntries, NewEntry)
		else
			break
		end
		]]
	end 
	
	return PageEntries
	
end


I guess I could just go to sleep and wake up knowing immediately what to do.

1 Like

I’m not exactly sure what you are trying to do, if you just need to divide Table into sets of “PageSize” try this

local function get_page(Table, pageNumber, pageSize)
    local out = {}
    for i = 1, pageSize do
        local index = pageNumber * pageSize + i
        local page = Table[index]
        if page then
            table.insert(out, page)
        end
    end

    return out
end

-- if you want to pre-compute Dictionary
local Dictionary = {}
do
    local i = 0
    local page = get_page(Table,  i, 3)
    while #page > 0 do
        table.insert(Dictionary, page)
        i += 1
        page = get_page(Table, i , 3)
    end
end
1 Like

Thanks!
I’m just wondering what this Pre-Computing thing does.
Cause it stats with “do” and nothing in front of it.

“do” declares a new scope, like a function that runs immediately and cannot be called again. I use it to reduce variables and enable code folding in the editor.

How would you make it into a function?

That depends on what you want to get out of it being a function. If you want to generate the whole dictionary as a function you could convert it like so, simply wrapping the whole thing in a function.

local function make_dictionary(Table, pageSize)
	local function get_page(pageNumber: number)
		local out = {}
		for i = 1, pageSize do
			local index = pageNumber * pageSize + i
			local page = Table[index]
			if page then
				table.insert(out, page)
			end
		end

		return out
	end

	local Dictionary = {}
	local i = 0
	local page = get_page(i)
	while #page > 0 do
		table.insert(Dictionary, page)
		i += 1
		page = get_page(i)
	end
	return Dictionary
end
1 Like