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

[help] tables and printing table contents

Started by Exerro, 29 July 2012 - 12:35 PM
Exerro #1
Posted 29 July 2012 - 02:35 PM
im trying to have a thing where you type in something it adds it to a table and then prints the table in a nice orderly fashion onto a monitor and i dont have a clue how to work with tables…the code i have so far is:
old code:
Spoiler

local food = {}
print("What Would You Like To Order Today?:")
local text = read()
table.insert(food, 1, text)
yourmonitor = peripheral.wrap("left") monitor
term.redirect(yourmonitor)
term.clear()
print("Last order:  ")
for i = 1,#food ipairs do
print(i)
end
term.restore()
print("Thank you for ordering: " text)
its saying theres a problem on line 9 which is no surprise coz i have no idea what it means

edit:
now i have this code that works but all it prints is 1 and 2 and 3…and more every time i enter something

local food = {}
while true do
term.clear()
print("What Would You Like To Order Today?:")
local text = read()
table.insert(food, 1, text)
yourmonitor = peripheral.wrap("left")
term.redirect(yourmonitor)
clear()
write("Last order:  ")
for i=1,#food do
print(i)
end
term.restore()  
print("Thank you for ordering: " ..text)
sleep(3)
end
thanks in advance…
BigSHinyToys #2
Posted 29 July 2012 - 02:52 PM
here
Spoiler

local food = {}
print("What Would You Like To Order Today?:")
local text = read()
table.insert(food, 1, text)
yourmonitor = peripheral.wrap("left")
term.redirect(yourmonitor)
term.clear()
print("Last order:  ")
for k,v in ipairs(food) do
print(v)
end
term.restore()
print("Thank you for ordering: "..text)
http://www.lua.org/manual/5.1/manual.html#pdf-pairs
ChiknNuggets #3
Posted 29 July 2012 - 02:57 PM
well line 9 is clear() if your trying to clear the monitor do yourmonitor.clear()
Exerro #4
Posted 29 July 2012 - 03:01 PM
ok ive edited my code and it it worked fine for a bit but now ive tried adding another table in it breaks at line 10

local food = {}
local tablenumbers = {}
while true do
clear()
print("What Would You Like To Order Today?:")
local text = read()
table.insert(food, 1, text)
print("What is your table number?:")
local table = read()
table.insert(tablenumbers, 1, table)
yourmonitor = peripheral.wrap("left")
term.redirect(yourmonitor)
clear()
write("Last order:  ")
for i = 1,#food do
write(tablenumbers[i].." - ")
print(food[i])
end
term.restore()
write("Thank you for ordering: " ..text)
sleep(3)
end
whats wrong with this???
BigSHinyToys #5
Posted 29 July 2012 - 03:12 PM
here
Spoiler

local food = {}
local tablenumbers = {}
yourmonitor = peripheral.wrap("left")
while true do
term.restore()
term.clear()
term.setCursorPos(1,1)
print("What Would You Like To Order Today?:")
local text = read()
table.insert(food, 1, text)
print("What is your table number?:")
local tablea = read()
table.insert(tablenumbers, 1, tablea)
term.redirect(yourmonitor)
term.clear()
term.setCursorPos(1,1)
write("Last order:  ")
for i = 1,#food do
write(tablenumbers[i].." - ")
print(food[i])
end
term.restore()
write("Thank you for ordering: " ..text)
sleep(3)
end
Exerro #6
Posted 29 July 2012 - 03:30 PM
ok thanks…fixed code is
Spoiler

local function clear()
term.clear()
term.setCursorPos(1,1)
end
local function printLine(x,y,text)
term.setCursorPos(x,y)
print(text)
end
local food = {}
local tablenumbers = {}
while true do
term.clear()
term.setCursorPos(1,1)
print("What Would You Like To Order Today?:")
local text = read()
table.insert(food, 1, text)
print("What is your table number?:")
local tablea = read()
table.insert(tablenumbers, 1, tablea)
yourmonitor = peripheral.wrap("left")
term.redirect(yourmonitor)
term.clear()
term.setCursorPos(1,1)
printLine(1,1,"Table: ")
for k = 1,#tablenumbers do
print(tablenumbers[k])
end
printLine(25,1,"Last order:  ")
for i = 1,#food do
printLine(25,1+i,food[i])
end
term.restore()
write("Thank you for ordering: " ..text)
sleep(3)
end

this is also copyrighted to me and programcraft inc but thanks for helping
BigSHinyToys #7
Posted 29 July 2012 - 03:40 PM
ok thanks…fixed code is
Spoiler

local function clear()
term.clear()
term.setCursorPos(1,1)
end
local function printLine(x,y,text)
term.setCursorPos(x,y)
print(text)
end
local food = {}
local tablenumbers = {}
while true do
term.clear()
term.setCursorPos(1,1)
print("What Would You Like To Order Today?:")
local text = read()
table.insert(food, 1, text)
print("What is your table number?:")
local tablea = read()
table.insert(tablenumbers, 1, tablea)
yourmonitor = peripheral.wrap("left")
term.redirect(yourmonitor)
term.clear()
term.setCursorPos(1,1)
printLine(1,1,"Table: ")
for k = 1,#tablenumbers do
print(tablenumbers[k])
end
printLine(25,1,"Last order:  ")
for i = 1,#food do
printLine(25,1+i,food[i])
end
term.restore()
write("Thank you for ordering: " ..text)
sleep(3)
end

this is also copyrighted to me and programcraft inc but thanks for helping

Well I want a commission for my "Consulting Work" then…$$$ lol XD