This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
KrisRevi's profile picture

textutils.serialize and tables

Started by KrisRevi, 02 July 2016 - 10:10 AM
KrisRevi #1
Posted 02 July 2016 - 12:10 PM
I have a adv computer hooked up to a Me Drive (from AE2) via cable and wired modems everything is working and i get the info i need BUT i dont know how to handle/split/get the info i want


-- This is hooked up via cable and wired modems to a Me Drive (from Applied Energistics)
MED = peripheral.wrap("tiledrive_1")

-- getAvaiableItems is a method for AE Me Drive to get info about the HDDs
getAllStacks = MED.getAvailableItems()

-- here i use textutils.serialize to be able to read the table(s) there is 1 table for each item in the Me Terminal
test = textutils.serialize(getAllStacks)

print(test)

This is how the print looks like

{
	is_fluid = false,
	size = 4430,
	is_item = true,
	is_craftable = false,
	fingerprint = {
		id = "name of block",
		dmg = 56,
	},
},

all i want out from each table is the size = number <— i want the numbers and then i want to add them together so i can match them up against total space i have on the Me Drive but i dont know how?!!

keep in mind im not a hard coder at LUA/CC
Edited on 02 July 2016 - 10:11 AM
Bomb Bloke #2
Posted 02 July 2016 - 12:42 PM
If that code directly produces that printout, then MED.getAvailableItems() must have multiple return values. You could bundle them up into the one table like this:

getAllStacks = { MED.getAvailableItems() }

Printing a serialised version would then look something like this:

{
	[1] = {
		is_fluid = false,
		size = 4430,
		is_item = true,
		is_craftable = false,
		fingerprint = {
			id = "name of block",
			dmg = 56,
		},
	},

	[2] = {
		is_fluid = false,
		size = 4430,
		is_item = true,
		is_craftable = false,
		fingerprint = {
			id = "name of other block",
			dmg = 56,
		},
	},

	[3] = {
		is_fluid = false,
		size = 4430,
		is_item = true,
		is_craftable = false,
		fingerprint = {
			id = "name of yet another block",
			dmg = 56,
		},
	},

	-- etc for each item
}

You'd be able to get the total size of each like this:

local totalSize = 0

for i = 1, #getAllStacks do
	totalSize = totalSize + getAllStacks[i].size
end

print(totalSize)

Note that we're not using textutils.serialise() there. That function can make it easier to represent or transmit tables sometimes, but it also makes it harder to actually use table content.

If the table layouts / access thing still isn't making sense to you, have a read through this and this. If you're unfamiliar with "for" loops, read this too.
Edited on 02 July 2016 - 10:42 AM
KrisRevi #3
Posted 02 July 2016 - 01:17 PM
———
Edited on 03 July 2016 - 09:45 AM
Bomb Bloke #4
Posted 03 July 2016 - 01:28 AM
In that case, start out by confirming the table structure is as I predicted:

local MED = peripheral.wrap("tiledrive_1")
textutils.pagedPrint( textutils.serialise( { MED.getAvailableItems() } ) )

Maybe some of the return values don't have "sizes"… or just maybe you misquoted your original output.