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

Paintutils and my loops

Started by Wing, 23 February 2013 - 06:05 PM
Wing #1
Posted 23 February 2013 - 07:05 PM
Hi guys, I would like to know if anyone can fix this, at the moment, only 1 pixel is being drawn and will eventually give: paintutils To long without yielding, blah blah….
Can anyone help me?
Also how does the "for" loop thing work?

os.loadAPI("w")
function checkColour(number)
if (number == 0) then
return 32768
elseif (number == 1) then
return 2048
elseif (number == 2) then
return 16384
elseif (number == 3) then
return 16
elseif (number == 4) then
return 8192
elseif (number == 5) then
return 2
elseif (number == 6) then
return 1024
elseif (number == 7) then
return 64
elseif (number == 8) then
return 128
elseif (number == 9) then
return 1
elseif (number == nil) then
return 32768 
end
end
function imageRead(dir,file)
term.clear()
term.setCursorPos(1,1)
local iline = 1
local ichar = 1
local ix = 1
local iy = 1
repeat
local dline = w.split(w.read(dir,file,iline),",")
repeat
local colour = checkColour(tonumber(dline[ichar]))
paintutils.drawPixel(ix,iy,colour)
local ichar = ichar +1
local ix = ix +1
until (dline[ichar] == nil)
local iline = iline +1
local iy = iy +1
until (dline == nil) 
end
imageRead("/","image")
ChunLing #2
Posted 24 February 2013 - 03:03 AM
This line:
local ichar = ichar +1
ichar already exists, you create a new copy every time, initializing it equal to the old value +1 (or 1+1). Assuming that w.split(w.read(dir,file,iline),",") returned a table in which the second element is not nil (which evidently it does), you have an infinite loop.

Remove the local keyword for variables that have already been declared.
Wing #3
Posted 24 February 2013 - 07:21 AM
Thanks I'll try this!
Wing #4
Posted 24 February 2013 - 07:30 AM
ok well, I did what you told me, here what it looks like:

function imageRead(dir,file)
term.clear()
term.setCursorPos(1,1)
local iline = 1
local ichar = 1
local ix = 1
local iy = 1
repeat
  local dline = w.split(w.read(dir,file,iline),",")
  repeat
   local colour = checkColour(tonumber(dline[ichar]))
   paintutils.drawPixel(ix,iy,colour)
   ichar = ichar +1
   ix = ix +1
  until (dline[ichar] == nil)
  iline = iline +1
  iy = iy +1
until (dline == nil)
end
Now it crashes inside my API, here are the API functions:
(w:6: attempt to index ? (a nil value))
Also it would be nice if you could fix the split function to also work without a pattern, just cuts everything you know, I can get it to work like that :D/>

function split(str,pat)
local t = {}
local fpat = "(.-)" .. pat
local last_end = 1
local s, e, cap = str:find(fpat, 1)
while s do
  if s ~= 1 or cap ~= "" then
  table.insert(t,cap)
  end
last_end = e+1
s, e, cap = str:find(fpat, last_end)
end
if last_end <= #str then
  cap = str:sub(last_end)
  table.insert(t, cap)
  end
return t
end
function read(path,file,line)
	local location = fs.combine(path,file)
	fs.exists(location)
	if (fs.exists(location) == true) then
		local get = fs.open(location,"r")
			acquiredinfo = {}
			repeat
				local info = get.readLine()
				table.insert(acquiredinfo, info)
			until (info == nil)
			get.close()
		if (acquiredinfo[line] ~= nil) then
			return acquiredinfo[line]
		else
			return nil
			end
	else
		return nil, "FILE DOESN'T EXIST"
		end
end
Sample of a line from "image":

1,1,1,8,1,1,8,1,1,1,8,1,1,8,1,1,1,8,1,1,8,1,1,1,8,1,1,8,1,1,1,8,1,1,8,1,1,1,8,1,1,8,1,1,1,8,1,1,8,1,1,1,8,1,1,8,1,1,1,8,1,1,8,1,1,1,8,1,1,1,1
Wing #5
Posted 24 February 2013 - 07:41 AM
NOTE FIXED, although would still love for someone to add the function of leaving the pattern argument empty!
ChunLing #6
Posted 24 February 2013 - 08:41 AM
local fpat = "(.-)" .. (pat or "")

Should work.
Wing #7
Posted 24 February 2013 - 09:20 AM
ya that doesn't work, I call it like this, a = w.split("this string", "")
"a" is just empty, it doesn't split anything!
ChunLing #8
Posted 24 February 2013 - 08:46 PM
Well, I don't know what you want it to do if a pattern isn't supplied, though. I just avoided the concatenate nil error. Do you want a default pattern?