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

[Question] Reading Tables Across Programs

Started by mikey53591, 01 January 2013 - 02:37 PM
mikey53591 #1
Posted 01 January 2013 - 03:37 PM
Ok, i'm stumped. I can't find any good tutorials and i can't seem to figure it out, so i'm gonna ask the pros. I'm creating a program that can dig a 3x3 tunnel, but i'm breaking each function into a different program for orginization's sake, i.e: Dig - the digging loop ; Return - return to inventory, etc. So my question is… How do i read tables that i start in one program in another program?

Here's my inital program for the 3x3 (the overall program that i'll run to start the whole digging process):
Spoiler

local t={}  --- Table i want to read---
term.clear()
term.setCursorPos(1,1)
print[[3x3 Tunnel Program v1.0
By Mike

]]
term.write("Length of Tunnel? ")
l=tonumber(read())
if l==0 then
  print[[
Length must be at least 1
]]
  sleep(2)
  os.run({}, "tunnel/3x3")
else
  print("\nLength of Tunnel: "..l)
  table.insert(t,1,l)
  print("\nStarting to Dig...")
  sleep(1)
  os.run({}, "tunnel/Dig")
end

The code's very much unfinished because i've yet to define the rest of the functions, but you get the idea hopefully.
The help's much appreciated guys. Thanks in advance :)/>
KaoS #2
Posted 01 January 2013 - 07:19 PM
you can pass them a custom environment or just make your tables global


local mytable={'val1','val2'}
local programname='test'
os.run({mytable=mytable},programname)

that way mytable would be accessible in the 'test' program
mikey53591 #3
Posted 02 January 2013 - 05:14 AM
you can pass them a custom environment or just make your tables global


local mytable={'val1','val2'}
local programname='test'
os.run({mytable=mytable},programname)

that way mytable would be accessible in the 'test' program

I know i'm being a pain, but could you give a more elaborate example? I'm pretty new to multiple program coding. I made a successful 3x3 tunnel digger before, but it was all in one big program. Just need a little more explination :o/>
ChunLing #4
Posted 02 January 2013 - 04:17 PM
Just declare the tables globally.

os.run() takes an environment table and a program to run in that environment. In this case, KaoS passed an environment {mytable=mytable}, that is, an environment in which mytable (defined in the scope from which the os.run call was made) was assigned to the identifier mytable, meaning that you could access mytable the table using the identifier mytable from within that environment.

If you don't understand what I just said, you're not alone. Just declare the table globally by omitting that local keyword.