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

Trying to make printing system using tables

Started by Snipa299, 26 December 2013 - 06:57 AM
Snipa299 #1
Posted 26 December 2013 - 07:57 AM
I've been trying to write a program that randomly generates 5 random serial numbers 1 - 1000000000 and puts them on bank notes. The problem I'm having is storing all the serial numbers and printing them onto one page so I can keep track of them. I've been using tables, but the page that is supposed to have the serial numbers always turns up blank. I'll write the code below.

p = peripheral.wrap("bottom")
local Serial = {}
l=1
q = 0
print "What value is the banknote?"
val = read()
print("making 5  " .. val .. " banknotes.")
i = (p.getInkLevel() > 5)
pa = (p.getPaperLevel() > 5)
if i == true and pa == true then
  repeat
	  p.newPage()
	  p.setPageTitle(val .." banknote")
	  ser = math.random(1000000000)
	  Serial[l] = ser
	  l = l + 1
	  p.write(ser .. " " .. val .. "s")
	  p.endPage()
	  q = q + 1
	  print(q)
	  sleep(2)
  until q == 5
  else
	print "Insert paper and ink then try again!"
	sleep(2)
	os.reboot()
  end
t = 1
h = 1
p.newPage()
p.setPageTitle("Serial Numbers")
repeat
  p.setCursorPos(1, h)
  w = Serial.t
  p.write(w)
  t = t + 1
  h = h + 1
until t == 6
  p.endPage()
  print "All done!"
  sleep(1)
  term.clear()
  term.setCursorPos(1, 1)

Any ideas or help?
MudkipTheEpic #2
Posted 26 December 2013 - 04:37 PM
I've been trying to write a program that randomly generates 5 random serial numbers 1 - 1000000000 and puts them on bank notes. The problem I'm having is storing all the serial numbers and printing them onto one page so I can keep track of them. I've been using tables, but the page that is supposed to have the serial numbers always turns up blank. I'll write the code below.

p = peripheral.wrap("bottom")
local Serial = {}
l=1
q = 0
print "What value is the banknote?"
val = read()
print("making 5  " .. val .. " banknotes.")
i = (p.getInkLevel() > 5)
pa = (p.getPaperLevel() > 5)
if i == true and pa == true then
  repeat
	  p.newPage()
	  p.setPageTitle(val .." banknote")
	  ser = math.random(1000000000)
	  Serial[l] = ser
	  l = l + 1
	  p.write(ser .. " " .. val .. "s")
	  p.endPage()
	  q = q + 1
	  print(q)
	  sleep(2)
  until q == 5
  else
	print "Insert paper and ink then try again!"
	sleep(2)
	os.reboot()
  end
t = 1
h = 1
p.newPage()
p.setPageTitle("Serial Numbers")
repeat
  p.setCursorPos(1, h)
  w = Serial.t
  p.write(w)
  t = t + 1
  h = h + 1
until t == 6
  p.endPage()
  print "All done!"
  sleep(1)
  term.clear()
  term.setCursorPos(1, 1)

Any ideas or help?

Well, the reason you are getting the problem is because you are overwriting the first index of the table every iteration. Speaking of iterations, have you ever heard of a 'for' loop? They are useful in situations like yours where you have do do something x amount of times.

For the first loop, instead of assigning q to 0 and adding 1 to it, you could let the for loop do the work for you, like so:

--#You don't need to define q beforehand, nor make it local.
for q=1,5 do --#Start at one, and for every iteration add one to q until q is greater then 5.
--#Do whatever you want here.
--#You can use Serial[q]=whatever for convenience.
end --#End the for loop

You can do the same for the second loop.

Also, instead of doing i==true and pa==true, just do i and pa. When you want to check if a boolean is true, just do if bool, not if bool==true. The reason for this is that anything that is not nil or false evaluates to true.
Edited on 26 December 2013 - 03:46 PM
Snipa299 #3
Posted 26 December 2013 - 06:50 PM
Well, I put it in, and the serial numbers page is finally printing. Now the only problem is that the numbers don't match up to the numbers on the banknotes. I'll post the code again

p = peripheral.wrap("bottom")
local Serial = {}
l=1
q = 0
print "What value is the banknote?"
val = read()
print("making 5  " .. val .. " banknotes.")
i = (p.getInkLevel() > 5)
pa = (p.getPaperLevel() > 5)
if i == true and pa == true then
  for q=1,5 do
    p.newPage()
    p.setPageTitle(val .." banknote")
    ser = math.random(1000000000)
    Serial[l] = ser
    p.write(Serial[l] .. " " .. val .. "s")
    l = l + 1
    p.endPage()
    print(q)
    sleep(2)
  end
else
  print "Insert paper and ink then try again!"
  sleep(2)
  os.reboot()
end
h = 1
p.newPage()
p.setPageTitle("Serial Numbers")
for t=1,5 do
  p.setCursorPos(1, h)
  w = Serial[h]
  p.write(w)
  h = h + 1
end
  p.endPage()
  print "All done!"
  sleep(1)
  term.clear()
  term.setCursorPos(1, 1)
Buho #4
Posted 27 December 2013 - 11:26 AM
Haven't parsed the whole thing, but near the end you have for t=1,5 do but never use the variable t. Shouldn't you have w = Serial[t]?

By the way, cool idea printing money!

EDIT: Sorry, that's not the problem. You are incrementing with l (get rid of this, just use q) and h (get rid of this, just use t). I think if you clean up your variables—even going so far as to rename them to something sensical—your problem will clear up. Parsing with my eyes, all looks in order. You are setting Serial[] and then getting Serial[] in the 1-5 range just fine.

EDIT2: You can use a For variable (I suggest i or x for a banal index) multiple times. It is locally scoped inside the For loop, so you can reuse it multiple times with no problems.
Edited on 27 December 2013 - 10:38 AM
Snipa299 #5
Posted 28 December 2013 - 07:20 AM
Thanks yet again. I haven't tryed this yet, but I'll give feedback on the results when I finally get around to modifying the program again.