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

How do you print a table on more than one page?

Started by DannySMc, 04 July 2014 - 08:47 PM
DannySMc #1
Posted 04 July 2014 - 10:47 PM
Okay so I have a table that can have lots and lots of entries, the thing is at some point it is going to be over the amount of the size of the screen, so how do I make either a button or a mouse_scroll to move it to the next page? the only thing I want to change is the data printed, things are printed before an after? so how could I do this? Here is my code? it uses a database, at the moment there isn't many entries, just need help in he future…


function database()
print("------------ All stored gate addresses ------------")
if not fs.exists("addresses") then
  print("No database found!")
  sleep(2)
  main()
end
a = db.load("addresses")
if #a < 15 then
  for k, v in ipairs(a) do
   print(k..": "..v)
  end
else
  print("> Database to big..")
end
print("---------------- Press 'b' to exit ----------------")
print("------------ Press 'd' to dial address ------------")
print("----------- Press 'r' to remove address -----------")
while true do
  local event, arg = os.pullEvent("char")
  if arg == "b" then
   main()
  elseif arg == "d" then
   print("> What address? (Use number)")
   write("> ")
   local toDial = tonumber(read())
   gate.dial(a[toDial])
   sleep(1)
   database()
  elseif arg == "r" then
   print("> What address do you wish to remove? (Use number)")
   write("> ")
   local toRemove = tonumber(read())
   db.removeString("addresses", a[toRemove])
   database()
  end
end
end
KingofGamesYami #2
Posted 04 July 2014 - 11:49 PM
This isn't an ideal solution, but it does work.

for k, v in ipairs( a ) do
 print( k..": "..v )
 os.pullEvent( "key" ) --#wait for the user to press any key
end
Lyqyd #3
Posted 04 July 2014 - 11:56 PM
Take a read through the textutils API. The paged tabulate function may give you some ideas.
TheOddByte #4
Posted 05 July 2014 - 12:20 AM
Well one solution I can think of is to create a function that formats a table into a table with tables ( ok that's hard to explain, I'll show you what I mean )

So for example if you have a table with strings

local t = {
    "foo";
    "bar";
    "Hello";
    "World!";
}
the function would format the table into a table that looks like this, where each table would represent a "page" depending on how many entries can be on each page( in this example it would be 2 )

local t = {
    {
        "foo";
        "bar";
    };
    {
        "Hello";
        "World!";
    };
}
then you could loop through the page you want

local page = 1 -- Let's just say we're on page 1
for i, v in ipairs( a[page] ) do
    print( i .. ": " .. v )
end
DannySMc #5
Posted 05 July 2014 - 12:23 AM
Well one solution I can think of is to create a function that formats a table into a table with tables ( ok that's hard to explain, I'll show you what I mean )

So for example if you have a table with strings

local t = {
	"foo";
	"bar";
	"Hello";
	"World!";
}
the function would format the table into a table that looks like this, where each table would represent a "page" depending on how many entries can be on each page( in this example it would be 2 )

local t = {
	{
		"foo";
		"bar";
	};
	{
		"Hello";
		"World!";
	};
}
then you could loop through the page you want

local page = 1 -- Let's just say we're on page 1
for i, v in ipairs( a[page] ) do
	print( i .. ": " .. v )
end

It's a good idea, but the table with the addresses will be added to constantly? so I really don't know?
TheOddByte #6
Posted 05 July 2014 - 12:51 AM
It's a good idea, but the table with the addresses will be added to constantly? so I really don't know?
Well lets say you have a table that contains all the stuff, lets call it 'a' and then you assign the formatted version of that table to a table called 'b' which will be the one that's drawn

local a = {}
while true do
    a = getStuff()
    b = format( a )
end