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

Table read then compair all

Started by Mr_Programmer, 11 January 2016 - 05:12 PM
Mr_Programmer #1
Posted 11 January 2016 - 06:12 PM
Hi all,

Is there away i can have a table, say hold all allowed fuel names, would i somehow be able to read that whole table and then do…

fuelcheck = turtle.getItemDetail()

if fuelcheck.name == --# some thing to do with then table and the fuel names inside, then
turlte.refuel()
end

Edited on 11 January 2016 - 05:13 PM
Lupus590 #2
Posted 11 January 2016 - 06:32 PM
if you make your table where the index is the item name and the value is anything but nil (true will do fine, or get smart and store the number of fuel units)

then you can do

local fuelList = {"minecraft:coal" = 80}
local item = turtle.getItemDetail()
if fuelList[item.name] then
  turtle.refuel()
end
Mr_Programmer #3
Posted 11 January 2016 - 06:42 PM
if you make your table where the index is the item name and the value is anything but nil (true will do fine, or get smart and store the number of fuel units)

then you can do

local fuelList = {"minecraft:coal" = 80}
local item = turtle.getItemDetail()
if fuelList[item.name] then
  turtle.refuel()
end

Dont want to store the fuel units lol, so
local fuelList = {"minecraft:coal", "minecraft:coal_block"} --# so i can put the name of any accepted fuel in here

if fuelList[item.name] then --# this will check every single string in the table and compair it to the getItemDetail

I what i said correct and will still work?

If i wanted to store it in a config file, would i have to do anything else but read the line and set it to a variable?
Lupus590 #4
Posted 11 January 2016 - 10:42 PM
I believe that your keys in the table will have a value of nil like that

do this just to be sure

local fuelList = {"minecraft:coal" = true, "minecraft:coal_block" = true}
Lyqyd #5
Posted 11 January 2016 - 10:57 PM
You'd need ["minecraft:coal"] = true for that.
Bomb Bloke #6
Posted 11 January 2016 - 11:21 PM
Indeed.

However, although building a table like this will "work":

local fuelList = {"minecraft:coal", "minecraft:coal_block"}

… the values representing the names will be stored as indexes 1 and 2. Meaning that fuelList[item.name] isn't going to find them - this is what Lupus is getting at.

Defining the table like this:

local fuelList = {["minecraft:coal"] = true, ["minecraft:coal_block"] = true}

… means that the names are the indexes, as the conditional check below is built to expect.
Mr_Programmer #7
Posted 11 January 2016 - 11:57 PM
Indeed.

However, although building a table like this will "work":

local fuelList = {"minecraft:coal", "minecraft:coal_block"}

… the values representing the names will be stored as indexes 1 and 2. Meaning that fuelList[item.name] isn't going to find them - this is what Lupus is getting at.

Defining the table like this:

local fuelList = {["minecraft:coal"] = true, ["minecraft:coal_block"] = true}

… means that the names are the indexes, as the conditional check below is built to expect.

So do i need the [] around the string or not as one example has [] and the other doesnt?
Bomb Bloke #8
Posted 12 January 2016 - 12:41 AM
Well, given that the second example is the one you should be using, and that's the one with the square brackets…

As for why they're needed, it comes down to context.

Again, when you build a table like this:

local fuelList = {"minecraft:coal", "minecraft:coal_block"}

… since you aren't specifying index names, Lua automatically assigns "minecraft:coal" to index 1, and "minecraft:coal_block" to index 2. These are simple strings that're going to be assigned as value, so they merely need quotes around them and that's that.

When you build a table like this:

local fuelList = {["minecraft:coal"] = true, ["minecraft:coal_block"] = true}

… you are defining the names - there's a key called "minecraft:coal", and there's a key called "minecraft:coal_block", and they both have the value of true.

Normally, when constructing a table this way, you don't need the quotes. You can do this:

local myTable = { someKey = someValue }

You can't do that with minecraft:coal, because of the colon. Lua sees that and assumes you're about to call a function from within a table (as that's what the symbol is used for within the language - it's a shorthand technique that passes the table itself as the first arugment to the function)..

But it you merely wrap it in quotes (to indicate that you're referring to a single string), then Lua's going to assume you're trying to add a string to the table (as in the first example). When you stick an equals after it it'll get confused, assume you made a mistake, and error out.

So, if you're going to use a key name with funny characters, you resort to wrapping square brackets around it. Quote it if it's a string, leave it unquoted if it's a number.
Dragon53535 #9
Posted 12 January 2016 - 02:41 AM
Why go through this long arduous process if you're looking for if an item is used as fuel for a turtle?


turtle.select(1)
local isFuel = turtle.refuel(0) --#Test the refuel, but don't use any items

If isFuel is true, then it's an item that can be used as fuel, if it's false, it can't.

That said, if you're wanting it to be for some OTHER device that uses fuel, then their solution above works perfectly for your needs. If it's turtle, this is as easy as you can get.
Edited on 12 January 2016 - 01:43 AM
Mr_Programmer #10
Posted 13 January 2016 - 01:19 AM
Why go through this long arduous process if you're looking for if an item is used as fuel for a turtle?


turtle.select(1)
local isFuel = turtle.refuel(0) --#Test the refuel, but don't use any items

If isFuel is true, then it's an item that can be used as fuel, if it's false, it can't.

That said, if you're wanting it to be for some OTHER device that uses fuel, then their solution above works perfectly for your needs. If it's turtle, this is as easy as you can get.
I acc never thought about this way, very handy and will save me alot of time! thanks