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

Railcraft tanks

Started by Dustmuz, 06 October 2014 - 07:16 AM
Dustmuz #1
Posted 06 October 2014 - 09:16 AM
after looking at my old post about this, and the link i got..

i just got more confused, yet again :D/> hehe..


im trying to get specific data out of a table, after i used the getTanksInfo()

and this is where im completely lost..
searched this forum on a tutorial on tables and found a few, and now im even more confused :D/>
i'm getting more lost the more i try to work with tables :(/>

but is there a way around this.

my tanks are wrapped as
lava1
lava2
fuel
oil
steam1
steam2

those are the "names" i gave all the tanks..

i am trying to implement it, into this program: pastebin

all help would be appriciated, so i dont end up trashing my tank :P/> hehe
Bomb Bloke #2
Posted 06 October 2014 - 09:37 AM
Let's say you've got a table, and you don't know what's in it. I usually respond by iterating through it and printing the contents:

for key,value in pairs(someTable) do
  print(key.." is a "..type(value))
end

Say you build this table:

local someTable = {43,{345,345},"SDFsf",3453,["somekey"] = "sdfsdf"}

If you ran the above code under that, it'd print something like this:

1 is a number
2 is a table
3 is a string
4 is a number
somekey is a string

That'd tell you that your table looks like this:

someTable[1] -- A number
someTable[2] -- A table
someTable[3] -- A string
someTable[4] -- A number
someTable["someKey"] -- A string

So if you wanted to print the string in someTable[3], you'd just do:

print(someTable[3])

And if you wanted to see what was in the table in someTable[2], you'd do:

for key,value in pairs(someTable[2]) do
  print(key.." is a "..type(value))
end

… and so on.
Dustmuz #3
Posted 06 October 2014 - 09:53 AM
Let's say you've got a table, and you don't know what's in it. I usually respond by iterating through it and printing the contents:

for key,value in pairs(someTable) do
  print(key.." is a "..type(value))
end

Say you build this table:

local someTable = {43,{345,345},"SDFsf",3453,["somekey"] = "sdfsdf"}

If you ran the above code under that, it'd print something like this:

1 is a number
2 is a table
3 is a string
4 is a number
somekey is a string

That'd tell you that your table looks like this:

someTable[1] -- A number
someTable[2] -- A table
someTable[3] -- A string
someTable[4] -- A number
someTable["someKey"] -- A string

So if you wanted to print the string in someTable[3], you'd just do:

print(someTable[3])

And if you wanted to see what was in the table in someTable[2], you'd do:

for key,value in pairs(someTable[2]) do
  print(key.." is a "..type(value))
end

… and so on.

okay.. i got lost somewhere between
line 1 and 2 :(/>

i tried printing (for good manners) the getTankInfo table and got the folowing
"table: 300204d8"

which i have no idea what means..
how can i print that table, so i can see what it contains, so that i can figure out, what to extract??

i get functions, peripherals, variables, loops, but tables. there i'm totally lost, you might aswell, drop me off in the middle of sahara and find my way back..
might actually be out of sahara before i get tables..
that part here, im stupid as a door :)/> sorry to admit it :)/>
Edited on 06 October 2014 - 08:03 AM
Dragon53535 #4
Posted 06 October 2014 - 10:40 AM
When you get that, all that means is that you tried printing a table as a whole which it doesn't like. If you wanted to get the TankInfo table you would need to use this

local tankInfo = getTankInfo() --#Change the GetTankInfo to what you use for it.
for key,value in pairs(tankInfo) do
  print("Use " .. key .. " to reference " ..value) --#This will tell you what variable to use to get the information, and the information itself.
--#You would want to use tankInfo[ whatever key you got ] to get the value at that spot.
--#If for instance a table looked like this tablename = {key1 = "cake",42} calling tablename[1] would give you 42, and tablename["key1"] or tablename.key1 will give you cake
--#If you don't supply a number or name infront of it, it starts at 1 and continues on for each one that doesn't have a key.
end
Bomb Bloke #5
Posted 06 October 2014 - 11:06 AM
That code'll only work if all "value"s of are of types that can be concatenated with strings. You could span it out so's the "value"s aren't concatenated with anything, but meh, I just print the types using the type() function.

i tried printing (for good manners) the getTankInfo table and got the folowing
"table: 300204d8"

which i have no idea what means..

It means the data you tried to print is a table pointer. That's an address indicating where the table's stored in memory.

You can't really straight-up "print a table". It's like trying to open up a chest and play a description of its contents on the violin. Yeah, there might be some actual sheet music in there, but how do you play its title? And what about the other bits of dust and fluff in there, how do you play that? No, when programming, you've got to be specific about what you want.

Bearing in mind that I've already given you perfectly good code for investigating what's in a table, there's also textutils.pagedTabulate(). I tend not to use it, as I can never remember the command off the top of my head and the output is usually excessive relative to what I actually want; plus it crashes out if certain data types (eg function pointers) are in your table.

If you haven't come across it, this document is well worth reading.
Dustmuz #6
Posted 06 October 2014 - 01:25 PM
okay, so.. if i understand anything right about this..

i should be able to do something like this??
lava1info = lava1.getTankInfo("unknown")

and then do what??

i tried to do
print(lava1info[3]) – this came up with a blank line

then i tried
for key, values in pairs(lava1info) do
print(key.." - "..values)
end
the last one here came up with an error (test:8:attempt to concatenate string and table)


the code i am testing on

lava1 = peripheral.wrap("rcsteeltankvalvetile_0")

lava1info = lava1.getTankInfo("unknown")

print(lava1info[3])
 
for key, value in pairs (lava1info) do
 print(key.." - "..value)
end
Bomb Bloke #7
Posted 06 October 2014 - 01:50 PM
At least one of the values in lava1info is another table.

You can't combine text such as " - " together with a table, hence the error message.

You could use this instead:

print(key.." - "..type(values))

That way, you'll at least know the key names, along with the types of data they have attached to them.
Dustmuz #8
Posted 06 October 2014 - 01:54 PM
At least one of the values in lava1info is another table.

You can't combine text such as " - " together with a table, hence the error message.

You could use this instead:

print(key.." - "..type(values))

That way, you'll at least know the key names, along with the types of data they have attached to them.

that line gives me
1 - table
2 - table

Sorry for not understanding any of this at all :(/>
Bomb Bloke #9
Posted 06 October 2014 - 02:26 PM
What it's telling you is that there are two entries in the "lava1info" table. One has a key of 1 (and can hence be referenced as lava1info[1]). The other has a key of 2 (and can hence be referenced as lava1info[2]). Both of these entries have another table assigned to them.

So let's say we want to know what's in the lavainfo[1] table. We'd run this:

for key, value in pairs (lava1info[1]) do
  print(key.." - "..type(value))
end
Dustmuz #10
Posted 06 October 2014 - 05:28 PM
What it's telling you is that there are two entries in the "lava1info" table. One has a key of 1 (and can hence be referenced as lava1info[1]). The other has a key of 2 (and can hence be referenced as lava1info[2]). Both of these entries have another table assigned to them.

So let's say we want to know what's in the lavainfo[1] table. We'd run this:

for key, value in pairs (lava1info[1]) do
  print(key.." - "..type(value))
end


tried doing what you said

table 1 gave me

Raw name - string
amount - number
capacity - number
name - string
id - number

and table 2 gave me
capacity - number


from this i can see, that the info i want to "take out" is in the table 1, place 2 and 3..
since i'm trying to do the same with the tanks, as i did with the resonant cells.. where i end up with a percent, that i can update and put on the monitor
Bomb Bloke #11
Posted 07 October 2014 - 02:04 AM
What it's telling you is that there are five entries in the "lava1info[1]" table. One has a key of "Raw name" (and can hence be referenced as lava1info[1]["Raw name"]). Another has a key of "amount" (and can hence be referenced as lava1info[1]["amount"]). Etc, etc, etc…

So let's say you wanted to print the capacity. You'd do this:

print(lava1info[1]["capacity"])
Dustmuz #12
Posted 07 October 2014 - 06:29 AM
What it's telling you is that there are five entries in the "lava1info[1]" table. One has a key of "Raw name" (and can hence be referenced as lava1info[1]["Raw name"]). Another has a key of "amount" (and can hence be referenced as lava1info[1]["amount"]). Etc, etc, etc…

So let's say you wanted to print the capacity. You'd do this:

print(lava1info[1]["capacity"])

lets see if i understand this correctly..

from start i would make a code that looks something like this


lava1 = *wrapping here*
lava2 = *wrapping here*

local function lava()
  lava1info = lava1.getTankInfo()
  lava2info = lava2.getTankInfo()
  lava1am = lava1info([1]["amount"])
  lava2am = lava2info([1]["amount"])
  lavatotal = lava1am + lava2am
  lava1cap = lava1info[1]["capacity"])
  lava2cap = lava2info[1]["capacity"])
  lavacap = lava1cap + lava2cap
  lavapercent = math.floor(lavatotal / lavacap * 100)
end

and then call that funcion in my while loop??
Dragon53535 #13
Posted 07 October 2014 - 06:38 AM
No. You are calling them as if they're functions. You do not want to use

lava1info([1]["amount"])
And should instead use

lava1info[1]["amount"]
--#Or you can use
lava1info.1.amount --#Although i'm not sure if it works with numbered keys.
Also at the end, you need to return the value rather than keep it inside the function (if you kept it all local :P/>)

return math.floor(lavatotal / lavacap * 100)
--# Instead of
lavapercent = math.floor(lavatotal / lavacap * 100)
Also i do believe i gave you a link to variable scope, it's easy to employ :P/>

Edit: Relinking since earlier link had search highlights. Here.
Edited on 07 October 2014 - 04:43 AM
Dustmuz #14
Posted 07 October 2014 - 06:45 AM
No. You are calling them as if they're functions. You do not want to use

lava1info([1]["amount"])
And should instead use

lava1info[1]["amount"]
--#Or you can use
lava1info.1.amount --#Although i'm not sure if it works with numbered keys.
Also at the end, you need to return the value rather than keep it inside the function (if you kept it all local :P/>)

return math.floor(lavatotal / lavacap * 100)
--# Instead of
lavapercent = math.floor(lavatotal / lavacap * 100)
Also i do believe i gave you a link to variable scope, it's easy to employ :P/>

it might be easy.. but i have no clue what so ever, what im doing with these tables.. hehe..

but i will try the changes you mention
Dragon53535 #15
Posted 07 October 2014 - 06:56 AM
You can view tables like this. A table written like this

local fakeTable = {"hi", [2] = "yaaaay!", "ddddddd",Keylist = "HEHEHEHEHE"}
Is basically

local fakeTable = {[1] = "hi", [2] = "yaaaay!", [3] = "dddddddd", ["Keylist"] = "HEHEHEHEHEHE"}
And so you could grab each part like

print(fakeTable[1]) --> "hi"
print(fakeTable[2]) --> "yaaaay!"
print(fakeTable[3]) --> "dddddddd"
print(fakeTable["Keylist"]) --> "HEHEHEHEHEHEHE"

Finally for nested tables that are written like

local multiTable = { { "Hi",Keyed = "Bye"}, secondTable = { "My second table rocks :D/>",Noo = "No it doesn't..."} }
Is really just

local multiTable = { [1] = { [1] = "Hi", ["Keyed"] = "Bye"}, ["secondTable"] = { [1] = "My second table rocks :D/>", ["Noo"] = "No it doesn't..."} }
And can be printed out like

print(multiTable[1][1]) --> "Hi"
print(multiTable[1]["Keyed"]) --> "Bye"
print(multiTable["secondTable"][1]) --> "My second table rocks :D/>"
print(multiTable["secondTable"]["Noo"]) --> "No it doesn't..."
Edited on 07 October 2014 - 04:57 AM
Dustmuz #16
Posted 07 October 2014 - 07:01 AM
now i tried rewriting it into the code i already have..
i get an error at line 61

attempt to concatenate function and string

pastebin
Spoiler

mon = peripheral.wrap("top")

mon.setBackgroundColor(colors.blue)
mon.clear()

cell1 = peripheral.wrap("cofh_thermalexpansion_energycell_237")
cell2 = peripheral.wrap("cofh_thermalexpansion_energycell_238")
cell3 = peripheral.wrap("cofh_thermalexpansion_energycell_239")
cell4 = peripheral.wrap("cofh_thermalexpansion_energycell_240")
cell5 = peripheral.wrap("cofh_thermalexpansion_energycell_241")
cell6 = peripheral.wrap("cofh_thermalexpansion_energycell_242")


lava1 = peripheral.wrap("rcsteeltankvalvetile_0")
lava2 = peripheral.wrap("rcsteeltankvalvetile_1")

local function lava()
  lava1info = lava1.getTankInfo("unknown")
  lava2info = lava2.getTankInfo("unknown")
  lava1am = lava1info[1]["amount"]
  lava2am = lava2info[1]["amount"]
  lavatotal = lava1am + lava2am
  lava1cap = lava1info[1]["capacity"]
  lava2cap = lava2info[1]["capatity"]
  lavacap = lava1cap + lava2cap
  return math.floor(lavatotal/lavacap * 100)
end

local function getPercent()
  local cellp1 = cell1.getEnergyStored("front")
  local cellp2 = cell2.getEnergyStored("front")
  local cellp3 = cell3.getEnergyStored("front")
  local cellp4 = cell4.getEnergyStored("front")
  local cellp5 = cell5.getEnergyStored("front")
  local cellp6 = cell6.getEnergyStored("front")
  local celltotal = cellp1 + cellp2 + cellp3 + cellp4 + cellp5 + cellp6
  local cellmax1 = cell1.getMaxEnergyStored("front")
  local cellmax2 = cell2.getMaxEnergyStored("front")
  local cellmax3 = cell3.getMaxEnergyStored("front")
  local cellmax4 = cell4.getMaxEnergyStored("front")
  local cellmax5 = cell5.getMaxEnergyStored("front")
  local cellmax6 = cell6.getMaxEnergyStored("front")
  local cellmax = cellmax1 + cellmax2 + cellmax3 + cellmax4 + cellmax5 + cellmax6
  return math.floor(celltotal / cellmax * 100)
end

mon.setCursorPos(1,1)
mon.write("Energy Control")
mon.setCursorPos(1,3)

while true do
  cellpercent = getPercent()
  if cellpercent < 10 then
	rs.setOutput("right",true)
  elseif cellpercent > 90 then
	  rs.setOutput("right",false)
  end
  mon.clearLine()
  mon.setCursorPos(1,3)
  mon.write("Energy level: "..cellpercent.."%")
  mon.write("Lava amount: "..lava.."%") --line 61 is THIS one
  sleep(2)
end

okay, i didnt find it :/
Edited on 07 October 2014 - 05:03 AM
Dragon53535 #17
Posted 07 October 2014 - 07:05 AM

mon.write("Lava amount: "..lava.."%")
--# Should be
mon.write("Lava amount: "..lava().."%")
Because basically putting lava says "hey this is a function, however i'm not going to use it" however calling it by adding the () says yeah we're using it and whatever it gives back, we're putting up.
Edited on 07 October 2014 - 05:06 AM
Dustmuz #18
Posted 07 October 2014 - 07:30 AM
oh okay.. will try that..
tried doing the same with lava as i did with the cell percent..

adding the lavapercent = lava()

but that just came up with another error

just tried doing it the way you suggest, and it came with the same error as the one i made..

power:25:attempt to perform arithmetic __add on number and nil
Dragon53535 #19
Posted 07 October 2014 - 07:35 AM

lava2cap = lava2info[1]["capatity"]
--#Should be
lava2cap = lava2info[1]["capacity"] --# You mispelled :P/>
Edited on 07 October 2014 - 05:35 AM
Dustmuz #20
Posted 07 October 2014 - 07:47 AM

lava2cap = lava2info[1]["capatity"]
--#Should be
lava2cap = lava2info[1]["capacity"] --# You mispelled :P/>/>

ooops :D/> hehe.. *tries to hide behind the flowerpot over there*

yay it works :D/>

so i should be able to do the same thing with the rest of my tanks.. nice :D/>

im defo gonna save this thread..
im not completely sure on the tables yet, but i might get there in time :D/>
Dustmuz #21
Posted 07 October 2014 - 09:02 AM
Just for showoff :D/>

This is how it looks when its done, and WORKING :D/>

so thanks A LOT for all the help

Bomb bloke
Dragon53535

i might not understand tables completely yet.. but im getting there :D/>
Dragon53535 #22
Posted 07 October 2014 - 09:09 AM
Good job on localizing your functions, however in any other part of your code you can say

print(lavacap)
And it will print out the lava cap.