ComputerCraft Table Fun

KingTriaxx

Forum Addict
Jul 27, 2013
4,266
1,333
184
Michigan
Okay, I don't know that Fun is necessarily the right word. Unless you're using the Dwarf Fortress definition.

In short, I'm looking to build a computer that can read certain information about pieces of my base from my house, so I don't have to wander around and find them. I've figured out how to get Tables into a readable format, but I can't figure out how to pull a specific value for sending to the display computer. The program I'm using now is this:

p = peripheral.wrap("bottom")

for i = 1,1 do
print (textutils.serialize(p.getTankInfo("bottom")))
end

I know how to send via modem, but I can't figure out how to extract the data from the table so I can display it. Can anyone point me in the right direction? I've searched the internet and the computercraft forums, to no avail. Thanks.
 

casilleroatr

New Member
Jul 29, 2019
1,360
0
0
You can reference each of the table components by their key if you know what they are, if you want to get a specific value from the table. Another better way of looking through the whole table is this

Code:
local p = peripheral.wrap("bottom")
local t = p.getTankInfo("botton")
for i,v in pairs(t) do
  print(i..": "..v)
end

It will look similar to the serialised printout assuming you are using the latest computercraft.

In that code I wrote, the i value represents the key and you can use that to call the specific element of a table that you want. In the following example I demonstrate this with a table I make e.g.

Code:
local t = {
  ["A"] = "Super",
  ["B"] = "Awesome",
  ["C"] = "Mega",
  }

--to reference a specific element follow this example

print(t["A"])
>Super

Experiment with the for i,v in pairs loops to work out what the keys are for all the different values you want from the tables you are using.
 

KingTriaxx

Forum Addict
Jul 27, 2013
4,266
1,333
184
Michigan
I had seen that Computer Craft post, but it's still complete gibberish.

I'll plug in that code and see if it helps.

Edit: I tried that code and what I get is 'attempt to concatenate string and table'.

The computer craft version in the mod pack says 1.57.
 
Last edited:

casilleroatr

New Member
Jul 29, 2019
1,360
0
0
I had seen that Computer Craft post, but it's still complete gibberish.

I'll plug in that code and see if it helps.

Edit: I tried that code and what I get is 'attempt to concatenate string and table'.

The computer craft version in the mod pack says 1.57.
If you get this error it probably means that the value stored in i or v (most likely v) is a table too.

An error free way of doing what you are already doing is this

Code:
for i,v in pairs(t) do
  write(i)
  write(": ")
  print(v)
end

This way there are no impossible concatenations. If you print a table on its own you will get something like this "table:474ff4dfrw3" which is of course unreadable but it won't error.
Before I continue I should mention that the for i,v in pairs() loop is an example of what is called a generic for loop.

If you encounter a table inside a generic for loop you can use another generic for loop to iterate through that table. You can do this forever. Here is an example.

Code:
--A table with subtables inside it
local t = {
  [1] = "Hello World",
  [2] = {
    ["Sub1"] = "I am",
    ["Sub2"] = "a",
    ["Sub3"] = "nested table.",
    ["Sub4"] = "Feel my wrath.",
    },
  [3] = "Goodbye world",
  }

--Iterate through the whole table including subtables in a way that handles strings and tables.

for i,v in pairs(t) do
  if type(v) == "string" then --type() is a standard Lua function which returns the type of a variable
    print(v)
  elseif type(v) == "table" then
    for k,w in pairs(v) do
       write(w.." ")
    end
  end
end

--Will result in this

>Hello World
>I am a nested table. Feel my wrath. 
>Goodbye world

Those open peripheral methods contain a hell of lot of nested tables. They are a good way of organising information even though they can be a pain to work out.

Another way of looking at the structure of a table is to serialize it using textutils.serialize() and then write the resulting string to a file. Here is how to do it if you are not familiar with the FS api.

Code:
local t = {
  --Pretend it is that table I wrote in the last example
  }

local serialT = textutils.serialize(t)

local file = fs.open("Table_Demo", "w") -- Creates a file object you can manipulate. "Table_Demo" is the save path for the file, "w" ensures you open it up in write mode. Other modes include "s" - save, and "a" - append
file.write(serialT) -- Writes text to a file
file.close() -- It is very important to use this function when you have finished changing the file or you start getting problems.

You can open the file you made either in game or on a notepad. Before CC 1.6 the serialised tables were difficult to read.
 
  • Like
Reactions: KingTriaxx

KingTriaxx

Forum Addict
Jul 27, 2013
4,266
1,333
184
Michigan
That explains it. I can see the method with nested tables being useful in things with multiple tanks, but since I'm looking at a Coke Oven with only one table, it's kind of pointless. Also it seems that simply changing the line to:

local t = p.getTankInfo("bottom")[1]

Works exactly as expected. Thanks for the help.

Edit: Okay, so now it works. New problem. I'm trying to set up so that the Status computer can call the computer on the tank and have the other computer say 'Okay, I've got this much in the tank.' I can get it to send the information manually, but not automatically.
 
Last edited:

KingTriaxx

Forum Addict
Jul 27, 2013
4,266
1,333
184
Michigan
It probably doesn't work because of the change from Liquids to Fluids. Anyway, I prefer to write my own code, because I can never understand what other people have written.
 

XLT_Frank

New Member
Jul 29, 2019
157
0
0
It probably doesn't work because of the change from Liquids to Fluids. Anyway, I prefer to write my own code, because I can never understand what other people have written.
It works thanks to some of the folks in the thread plus my changes. See pastebin link. It is wireless, auto detects, and supports multiple tanks.

Sent from my Galaxy Nexus using Tapatalk