Jump to content




Splitting a massive string to print with

peripheral help

27 replies to this topic

#21 samdeman22

  • Members
  • 101 posts
  • LocationScotland!

Posted 30 March 2013 - 08:21 AM

Aha, a new problem,
-- a function you cant access ;P
local function round(num, idp)
  local mult = 10 ^ (idp or 0)
  return (math.floor(num * mult + 0.5) / mult)
end
function printLog(log, side, fromLn, toLn)
  if open == true then
    error("Log must be ended before printing")
  end
  if not fs.exists( "logs/"..log..".log") then
    error("Log does not exist")
  end
 
  h = fs.open("logs/"..log..".log", "r")
  page = {}
  while h.readLine() ~= nil do
    local ln = h.readLine()
    table.insert(page, ln)
  end
 
  p = peripheral.wrap(side)
  p.newPage()
  local paper = p.getPaperLevel()
  local ink = p.getInkLevel()
  local w, h = p.getPageSize()
 
  local doc = {}
  for i = fromLn, toLn do
    table.insert(doc, page[i])
  end
 
  local length = 0
  for i = 0, #doc do
    length = length + #doc[i]
  end
 
  local size = w*h
  local pages = round(length/size, 0)
  if pages > paper then
    error("not enough paper")
  end
  if pages > ink then
    error("not enough ink")
  end
  for i = 0, pages do
  p.newPage()
  p.setPageTitle(log.." page:"..i)
    for indx = 0, size do
	  p.write(doc[indx])
    end
  p.endPage()
  end
 
end
"log:91 attempt to get length of nil" it says, any suggestions?

#22 faubiguy

  • Members
  • 213 posts

Posted 30 March 2013 - 08:31 AM

You're starting the loop
for i = 0, #doc do
  length = length + #doc[i]
end
at 0 instead of 1.
if there isn't a value at doc[0], it can't get the length of it.

#23 samdeman22

  • Members
  • 101 posts
  • LocationScotland!

Posted 30 March 2013 - 11:43 AM

View Postfaubiguy, on 30 March 2013 - 08:31 AM, said:

You're starting the loop
for i = 0, #doc do
  length = length + #doc[i]
end
at 0 instead of 1.
if there isn't a value at doc[0], it can't get the length of it.
I maybe thought about that briefly and then forgot it, thanks, will try this soon, expect to see the log API out soon!

#24 samdeman22

  • Members
  • 101 posts
  • LocationScotland!

Posted 30 March 2013 - 12:55 PM

nope, this time it actually runs, but it just fills the tray with 5 blank pages :/ Can anyone suggest a better print function to what I have? I need to be able to print a file from a certain line to a certain line so basically (log, side, fromLn, toLn) where log is the file, side is the printer, and from/toLn are the from/to line.

#25 samdeman22

  • Members
  • 101 posts
  • LocationScotland!

Posted 30 March 2013 - 12:57 PM

oh and you must use this at the beginning
  if open == true then
    error("Log must be ended before printing")
  end
  if not fs.exists( "logs/"..log..".log") then
    error("Log does not exist")
  end

  h = fs.open("logs/"..log..".log", "r")
ok :)

open is defined in the rest of the API

#26 samdeman22

  • Members
  • 101 posts
  • LocationScotland!

Posted 31 March 2013 - 01:31 AM

could the reason it prints blank pages be:
  for i = 0, pages do
  p.newPage()
  p.setPageTitle(log.." page:"..i)
    for indx = 0, size do
		  p.write(doc[indx])
    end
  p.endPage()
  end
should this be for i = 1, pages and indx = 1, size rather than 0, pages and 0, size?

#27 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 31 March 2013 - 10:13 AM

Yes.. doc table doesn't have a 0 index

#28 samdeman22

  • Members
  • 101 posts
  • LocationScotland!

Posted 02 April 2013 - 06:20 AM

View PostremiX, on 31 March 2013 - 10:13 AM, said:

Yes.. doc table doesn't have a 0 index
ok, still haven't had a chance to test this out but you know, I need to get it in my head "tables do not have a position:0". :)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users