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

Tables and Textutils :(

Started by Rougeminner, 04 April 2015 - 03:45 AM
Rougeminner #1
Posted 04 April 2015 - 05:45 AM
I am new to using tables. i have tried for years to stay away from them in every programing language i know, but they where created for a reason.

My Problem: I am making a program that has a list of Items it keeps and a list of items it throws away in mining, Said list is a whole other file name trashlist. I would like it to be formatted like this


{
"Gravel",
"CobbleStone",
"Dirt",
"Every other useless block you could mine in minecraft in the search for diamonds!"

}

i need to know how to make the code be able to read the very long list of contents in this table without there being a set limit to items. and how to i compare if the selected Item is on the trashlist. I am using turtle.getDetail()


Thanks in advance
Lyqyd #2
Posted 04 April 2015 - 05:48 AM
Open the file, then read the entire contents into a string and run it through textutils.unserialize(). That will return a table of your strings.
Rougeminner #3
Posted 04 April 2015 - 05:55 AM
ok and i can just do #table{} to get its length correct? Then how do i compare it with the Item info that the turtle has just received from the selected slot.

P.S. Thanks for the Early Reply
valithor #4
Posted 04 April 2015 - 06:23 AM
ok and i can just do #table{} to get its length correct? Then how do i compare it with the Item info that the turtle has just received from the selected slot.

P.S. Thanks for the Early Reply

There are certain circumstances in which #table would not return what you would expect, but in this scenario it should. You will not need the {} just #table (dont name your table "table" it causes problems).

As for comparing it you could use a for loop.


item = turtle.getItemDetail()
for i = 1, #tbl do
  if tbl[i] == item.name then
	-- it equals so do stuff
	break -- might as well break the loop nothing else will return true if we already found what it matches.
  end
end

This will get the number of entries in the table tbl, which will cause the for loop to loop that number of times. Each time it checks if the i'th (i increases by 1 each time it loops through so it would eventually reach every entry in the table) entry in the table is equal to the item if it is the if statement would return true and you could do something.
Edited on 04 April 2015 - 04:34 AM
Rougeminner #5
Posted 04 April 2015 - 06:27 AM
thanks for your help. I didn't name it table. i have enough experience in the complicated and personally hated land of Tables to not make that mistake. Thank you for you code i will test it now. With the code you have provided can i format the file as i listed above?
valithor #6
Posted 04 April 2015 - 06:31 AM
thanks for your help. I didn't name it table. i have enough experience in the complicated and personally hated land of Tables to not make that mistake. Thank you for you code i will test it now. With the code you have provided can i format the file as i listed above?

Yes that should work.
In this section you really can not assume anything, seen people name things table so many times, and have been yelled at many times for using a variable named table in my examples… lol

Ehh actually i do need to make one small correction to the code in the if statement use this instead


if tbl[i] == item.name then

Just know the name in the table has to match the name that turtle.getItemDetail returns exactly or it will not work.
Edited on 04 April 2015 - 04:44 AM
Rougeminner #7
Posted 04 April 2015 - 06:33 AM
ok. thanks for your example. nice modpack by the way i am going to download it tomorrow. this should get my code complete soon thanks for your help once more



EDIT: Thank you. i will make sure to account for that when i type it out.
Edited on 04 April 2015 - 04:34 AM