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

Need help with Tables

Started by Kezaraux, 13 April 2016 - 07:58 PM
Kezaraux #1
Posted 13 April 2016 - 09:58 PM
What I need help with specifically, is getting the amount of items stored inside of a Factorization barrel. Now, I've have had limited success so far. I can print all the information out that I need and see that the value I want is there, I just cannot for the life of me figure out how to access it!
Here is my code that I am using the get the information of the barrel.


local bar = peripheral.wrap("right")
local tableInf = bar.getAllStacks()
for i, j in pairs(tableInf) do
  print(i)
  for a, b in pairs(j) do
--	print(a)
	if (type(B)/>/>) ~= "table") then
	  print(a..": "..B)/>/>
--[[	else
	  for c, d in pairs(B)/>/> do
		print(c..": "..d)
	  end--]]
	end
  end
end

Edit: It seems to want to add in the "/>"s, which is annoying. Please ignore those and read all B's and b's.

Now, what I get at the end of this is:


1	(this comes from printing "i")
id: 17
name: Oak Wood
rawName: tile.log.oak
dmg: 0
maxSize: 64
qty: 0
2	(this comes from printing "i")
id: 17
name: Oak Wood
rawName: tile.log.oak
dmg: 0
maxSize: 64
qty: 1

I just included in there the values that get printed from "i", all the others are pairs from "j".

Now, from my limited understanding of LUA (I'm more used to Java or JavaScript), the table should look something like this…


tableInf = {
	["1"] = {
		id
		name
		rawName
		dmg
		maxSize
		qty
	}
	["2"] = {
		id
		name
		rawName
		dmg
		maxSize
		qty
	}
}

The value I want to access, is in the second table, the "qty" that equals 1. That is the actual value of how many logs I have stored inside this barrel. I don't know how to access it, and quite frankly I am VERY frustrated with this at the moment! A big thanks in advance to anyone who can help me out!
Edited on 13 April 2016 - 08:01 PM
KingofGamesYami #2
Posted 13 April 2016 - 10:08 PM
Given the output of your program, I can conclude that the value you are after is:


tableInf[ 2 ].qty

There are two problems with your representation of the table;
  • The indexes are numbers, not string (eg 1 is 1, not "1")
  • You did not display the values

A corrected version:

tableInf = {
        [1] = {
                id  = 17,
                name = "Oak Wood",
                rawName = "tile.log.oak",
                dmg = 0,
                maxSize = 64,
                qty = 0
        },
        [2] = {
                id = 17,
                name  = "Oak Wood",
                rawName = "tile.log.oak",
                dmg = 0,
                maxSize = 64
                qty = 1
        }
}
Dragon53535 #3
Posted 13 April 2016 - 10:16 PM
Pretty much what KoGY said. Also if you want to get rid of the B/> just add a space between the b and )


B)/>
b )

It's the forums stupid emoticons which it thinks means you want B)/> inside your code, realizes that it can't, and adds the /> to stop itself

At least I think, lets see: B)/>/>

Nope, the /> is probably it just screwing up with how it's trying to stop itself.
Edited on 13 April 2016 - 08:48 PM
Kezaraux #4
Posted 13 April 2016 - 10:35 PM
Given the output of your program, I can conclude that the value you are after is:


tableInf[ 2 ].qty

There are two problems with your representation of the table;
  • The indexes are numbers, not string (eg 1 is 1, not "1")
  • You did not display the values
A corrected version:

tableInf = {
		[1] = {
				id  = 17,
				name = "Oak Wood",
				rawName = "tile.log.oak",
				dmg = 0,
				maxSize = 64,
				qty = 0
		},
		[2] = {
				id = 17,
				name  = "Oak Wood",
				rawName = "tile.log.oak",
				dmg = 0,
				maxSize = 64
				qty = 1
		}
}

Awesome! Thank you for clearing that up for me! And yes, that allows me to get the value I want! Thanks for the help!

Pretty much what KoGY said. Also if you want to get rid of the B/> just add a space between the b and )


B)/>/>/>/>/>
b )

It's the forums stupid emoticons which it thinks means you want B)/> inside your code, realizes that it can't, and adds the /> to stop itself

At least I think, lets see: B)/>/>

Nope, the /> is probably it just screwing up with how it's trying to stop itself.

Ah, I thought it might be something like that. I'll hopefully remember that for the future. Thanks!
Edited on 13 April 2016 - 08:37 PM
Bomb Bloke #5
Posted 14 April 2016 - 12:30 AM
Pretty much what KoGY said. Also if you want to get rid of the B/> just add a space between the b and )

Better yet, switch to the full post editor (More Reply Options), disable emoticons, and just type it how you want it. b) b) b)