-- table expander --
os.loadAPI("ocs/apis/sensor")
term.clear()
term.setCursorPos(1,1)
local function expand(tab) --"tab" is a table derp
for k,v in pairs(tab) do
print(tostring(k)..": ")
if type(v) ~= "table" then
print(tostring(v))
else
return pcall(expand(v))--gets stuck at the end of one table...
end
end
end
local prox = sensor.wrap("top")
local targets = prox.getTargets()
local tDetails = prox.getTargetDetails("Molinko")
--expand(targets) --success?!
expand(tDetails)
Thanks for any help
Help with recursion.
Started by Molinko, Mar 18 2013 09:05 PM
8 replies to this topic
#1
Posted 18 March 2013 - 09:05 PM
I'm in need of some help with a recursive function.. I would like it to be able to print out an entire table and all included tables, however when i use this on very long tables it will hang eventually at the end of one of the sub tables. If you could help me figure out how to achieve this i would greatly appreciate it.
#2
Posted 19 March 2013 - 03:43 AM
Split into new topic.
#3
Posted 19 March 2013 - 03:53 AM
This is how pcall works...
But I don't see why you have to do that at all and the return. something like this should work fine.
You may want to add a sleep(0) at the end of each loop, just incase its going to take >10 seconds to iterate.
pcall(function_pointer, param1, param2, param3, ...)
But I don't see why you have to do that at all and the return. something like this should work fine.
local function expand(tab) --"tab" is a table derp for k,v in pairs(tab) do print(tostring(k)..": ") if type(v) ~= "table" then print(tostring(v)) else expand(v) end end end
You may want to add a sleep(0) at the end of each loop, just incase its going to take >10 seconds to iterate.
#4
Posted 19 March 2013 - 06:06 AM
TheOriginalBIT, on 19 March 2013 - 03:53 AM, said:
This is how pcall works...
But I don't see why you have to do that at all and the return. something like this should work fine.
You may want to add a sleep(0) at the end of each loop, just incase its going to take >10 seconds to iterate.
pcall(function_pointer, param1, param2, param3, ...)
But I don't see why you have to do that at all and the return. something like this should work fine.
local function expand(tab) --"tab" is a table derp for k,v in pairs(tab) do print(tostring(k)..": ") if type(v) ~= "table" then print(tostring(v)) else expand(v) end end end
You may want to add a sleep(0) at the end of each loop, just incase its going to take >10 seconds to iterate.
I was just using pcall to see when in the loop i was having an issue... But ive never used it before. I shouldve cleaned that out before posting... I added a sleep(0) in there too after my if statment and i works like a charm.. Thanks for the tip TheOriginalBit,
not sure why i didnt think of that :3
#5
Posted 19 March 2013 - 07:57 AM
Sorry to add a little off topic but whn i try and wtire this data to a monitor i have to use write(v) bu tI would like it to start a newline..
when i try to use a line escape it doesnt work... ive tried m.write(tostring(v).."\n") but it write a "?" to the screen instead of a line escape. thanks for your help and let me know if you need the code for this help. thank you
when i try to use a line escape it doesnt work... ive tried m.write(tostring(v).."\n") but it write a "?" to the screen instead of a line escape. thanks for your help and let me know if you need the code for this help. thank you
#6
Posted 19 March 2013 - 08:01 AM
The easiest way to do that is to use term.redirect:
local mon = peripheral.wrap("left") -- or whatever you use to wrap the monitor
term.redirect(mon) -- redirect output to the monitor
-- print whatever you want here, using write or print
print("Hello World!") -- example. This will be printed in the monitor
term.restore() -- restore output to the terminal
#7
Posted 19 March 2013 - 08:15 AM
MysticT, on 19 March 2013 - 08:01 AM, said:
The easiest way to do that is to use term.redirect:
local mon = peripheral.wrap("left") -- or whatever you use to wrap the monitor
term.redirect(mon) -- redirect output to the monitor
-- print whatever you want here, using write or print
print("Hello World!") -- example. This will be printed in the monitor
term.restore() -- restore output to the terminal
yeah i just figured this out thanks MysticT.. btw i had to use term.redirect(term.native) to restore control back to the terminal. just a heads up there. But for future reference is it possible to do the first way i was trying? or is it just not really practical??
#8
Posted 19 March 2013 - 09:16 AM
Molinko, on 19 March 2013 - 08:15 AM, said:
btw i had to use term.redirect(term.native) to restore control back to the terminal.
Molinko, on 19 March 2013 - 08:15 AM, said:
But for future reference is it possible to do the first way i was trying? or is it just not really practical??
#9
Posted 19 March 2013 - 10:27 AM
MysticT, on 19 March 2013 - 09:16 AM, said:
Molinko, on 19 March 2013 - 08:15 AM, said:
btw i had to use term.redirect(term.native) to restore control back to the terminal.
Molinko, on 19 March 2013 - 08:15 AM, said:
But for future reference is it possible to do the first way i was trying? or is it just not really practical??
2 user(s) are reading this topic
0 members, 2 guests, 0 anonymous users











