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

Help With FOR

Started by Creeper9207, 19 May 2015 - 08:56 PM
Creeper9207 #1
Posted 19 May 2015 - 10:56 PM

args = {...}
function dfb(x1,x2,y1,y2, c)
  paintutils.drawFilledBox(x1,x2,y1,y2,c)
end
f = fs.open(args[1], "r")
for line in f.readLine do
  if line:gmatch("sleep:") then
  parse = line:gsub("sleep:", "")
  sleep(tonumber(parse))
  end
  if line:gmatch("background=") then
   parse = line:gsub("background=", "")
   w, h = term.getSize()
   if parse == "white" then
	   dfb(1,1,w,h,colors.white)
   elseif parse == "magenta" then
	   dfb(1,1,w,h,colors.magenta)
   elseif parse == "light_blue" then
	   dfb(1,1,w,h,colors.lightBlue)
   elseif parse == "yellow" then
	   dfb(1,1,w,h,colors.yellow)
   elseif parse == "lime" then
	   dfb(1,1,w,h,colors.lime)
   elseif parse == "pink" then
	   dfb(1,1,w,h,colors.pink)
   elseif parse == "gray" then
	   dfb(1,1,w,h,colors.gray)
   elseif parse == "light_gray" then
	   dfb(1,1,w,h,colors.lightGray)
   elseif parse == "cyan" then
	   dfb(1,1,w,h,colors.cyan)
   elseif parse == "purple" then
	   dfb(1,1,w,h,colors.purple)
   elseif parse == "blue" then
	   dfb(1,1,w,h,colors.blue)
   elseif parse == "brown" then
	   dfb(1,1,w,h,colors.brown)
   elseif parse == "green" then
	   dfb(1,1,w,h,colors.green)
   elseif parse == "red" then
	   dfb(1,1,w,h,colors.red)
   elseif parse == "black" then
	   dfb(1,1,w,h,colors.black)
   elseif parse == "orange" then
	   dfb(1,1,w,h,colors.orange)
	 end
   end
   if line:gmatch("box=") then
	 parse = line:gsub("box=", "")
   c = {}
   c2 = 0
   c[c2] = ""
   for i7 = 1, #parse do
    local char = parse:sub(i7,i7)
    -- do something with c
    if char == " " then
	  c2 = c2 + 1
	  c[c2] = ""
    end
    c[c2] = c[c2] .. char
   end
   print(c[0])
   --for k, v in pairs(c) do
   -- print(v)
   --end
	 if parse == "white" then
	   dfb(1,1,w,h,colors.white)
	 elseif parse == "magenta" then
	   dfb(1,1,w,h,colors.magenta)
   elseif parse == "light_blue" then
	   dfb(1,1,w,h,colors.lightBlue)
   elseif parse == "yellow" then
	   dfb(1,1,w,h,colors.yellow)
   elseif parse == "lime" then
	   dfb(1,1,w,h,colors.lime)
   elseif parse == "pink" then
	   dfb(1,1,w,h,colors.pink)
   elseif parse == "gray" then
	   dfb(1,1,w,h,colors.gray)
   elseif parse == "light_gray" then
	   dfb(1,1,w,h,colors.lightGray)
   elseif parse == "cyan" then
	   dfb(1,1,w,h,colors.cyan)
   elseif parse == "purple" then
	   dfb(1,1,w,h,colors.purple)
   elseif parse == "blue" then
	   dfb(1,1,w,h,colors.blue)
   elseif parse == "brown" then
	   dfb(1,1,w,h,colors.brown)
   elseif parse == "green" then
	   dfb(1,1,w,h,colors.green)
   elseif parse == "red" then
	   dfb(1,1,w,h,colors.red)
   elseif parse == "black" then
	   dfb(1,1,w,h,colors.black)
   elseif parse == "orange" then
	   dfb(1,1,w,h,colors.orange)
	 end
   end
end
f.close()

Read File:


background=orange
sleep:1
background=red
sleep:0.5
box=hello

OK, so this should print "hello", but it prints all the lines of the read file for some reason! please help T_T
Edited on 19 May 2015 - 10:41 PM
Bomb Bloke #2
Posted 20 May 2015 - 01:47 AM
line:gmatch("whatever") returns an iterator function. For the purposes of an "if" statement, anything that isn't false/nil counts as true, so that returned function - which you aren't running, so the results of it are irrelevant here - is what's throwing out your checks. Use line:find("whatever") instead.

And try to avoid repetitive code!

if colors[parse] then dfb(1,1,w,h,colors[parse]) elseif colours[parse] then dfb(1,1,w,h,colours[parse]) end
Edited on 19 May 2015 - 11:48 PM
Creeper9207 #3
Posted 20 May 2015 - 01:59 AM
thanks man, everybody in irc was like "format it rite" when it was already correct XD
Bomb Bloke #4
Posted 20 May 2015 - 02:14 AM
Actually that formatting is pretty bad; but the script's simple enough that tracking the flow's pretty simple anyways. :P/>

Bear in mind that if you're having trouble getting the forum software to reflect the indentation of your original script file correctly, the key is to turn off the rich text editor. Of course, using pastebin is generally the better option for anything over a couple of dozen lines of code.
Creeper9207 #5
Posted 20 May 2015 - 02:50 AM

xc,yc = term.getCursorPos()
args = {...}
function dfb(x1,x2,y1,y2, c)
  paintutils.drawFilledBox(x1,x2,y1,y2,c)
end
function h(c0,c1,c2,c3,c4)
  tonumber(c0)
  tonumber(c1)
  tonumber(c2)
  tonumber(c3)
if c4 == "white" then
	   dfb(c0,c1,c2,c3,colors.white)
	 elseif c4 == "magenta" then
	   dfb(c0,c1,c2,c3,colors.magenta)
   elseif c4 == "light_blue" then
	   dfb(c0,c1,c2,c3,colors.lightBlue)
   elseif c4 == "yellow" then
	   dfb(c0,c1,c2,c3,colors.yellow)
   elseif c4 == "lime" then
	   dfb(c0,c1,c2,c3,colors.lime)
   elseif c4 == "pink" then
	   dfb(c0,c1,c2,c3,colors.pink)
   elseif c4 == "gray" then
	   dfb(c0,c1,c2,c3,colors.gray)
   elseif c4 == "light_gray" then
	   dfb(c0,c1,c2,c3,colors.lightGray)
   elseif c4 == "cyan" then
	   dfb(c0,c1,c2,c3,colors.cyan)
   elseif c4 == "purple" then
	   dfb(c0,c1,c2,c3,colors.purple)
   elseif c4 == "blue" then
	   dfb(c0,c1,c2,c3,colors.blue)
   elseif c4 == "brown" then
	   dfb(c0,c1,c2,c3,colors.brown)
   elseif c4 == "green" then
	   dfb(c0,c1,c2,c3,colors.green)
   elseif c4 == "red" then
	   dfb(c0,c1,c2,c3,colors.red)
   elseif c4 == "black" then
	   dfb(c0,c1,c2,c3,colors.black)
   elseif c4 == "orange" then
	   dfb(c0,c1,c2,c3,colors.orange)
	 end

end
f = fs.open(args[1], "r")
for line in f.readLine do
  if line:find("sleep:") then
  parse = line:gsub("sleep:", "")
  sleep(tonumber(parse))
  end
  if line:find("background=") then
   parse = line:gsub("background=", "")
   w, h = term.getSize()
   if parse == "white" then
	   dfb(1,1,w,h,colors.white)
   elseif parse == "magenta" then
	   dfb(1,1,w,h,colors.magenta)
   elseif parse == "light_blue" then
	   dfb(1,1,w,h,colors.lightBlue)
   elseif parse == "yellow" then
	   dfb(1,1,w,h,colors.yellow)
   elseif parse == "lime" then
	   dfb(1,1,w,h,colors.lime)
   elseif parse == "pink" then
	   dfb(1,1,w,h,colors.pink)
   elseif parse == "gray" then
	   dfb(1,1,w,h,colors.gray)
   elseif parse == "light_gray" then
	   dfb(1,1,w,h,colors.lightGray)
   elseif parse == "cyan" then
	   dfb(1,1,w,h,colors.cyan)
   elseif parse == "purple" then
	   dfb(1,1,w,h,colors.purple)
   elseif parse == "blue" then
	   dfb(1,1,w,h,colors.blue)
   elseif parse == "brown" then
	   dfb(1,1,w,h,colors.brown)
   elseif parse == "green" then
	   dfb(1,1,w,h,colors.green)
   elseif parse == "red" then
	   dfb(1,1,w,h,colors.red)
   elseif parse == "black" then
	   dfb(1,1,w,h,colors.black)
   elseif parse == "orange" then
	   dfb(1,1,w,h,colors.orange)
	 end
   end
   if line:find("box=") then
	 parse = line:gsub("box=", "") .. " "
   c = {}
   c2 = 0
   c[c2] = ""
   for i7 = 1, #parse do
	local char = parse:sub(i7,i7)
	-- do something with c
	if char == " " then
	  d = c[c2]:gsub(' ', '')
	  print(d)
	  c2 = c2 + 1
	  c[c2] = ""
	end
	c[c2] = c[c2] .. char
   end
	print("hi")
	h(c[0]:gsub(' ', ''),c[1]:gsub(' ', ''),c[2]:gsub(' ', ''),c[3]:gsub(' ', ''),c[4]:gsub(' ', ''))
   end
end
f.close()
term.setCursorPos(xc,yc)



Line 6, expected StartX, StartY, EndX, EndY, color but they are valid numbers! 2, 2, 5, 5, colors.purple
Edited on 20 May 2015 - 01:09 AM
KingofGamesYami #6
Posted 20 May 2015 - 02:54 AM
Hint: colors.red is the same as colors["red"], which means you can do this:

local var = "red"
colors[ var ] --#this is equivalent to colors.red

Also, you pass your function colors.<color> instead of the string, meaning it won't ever compare as true to "white", etc.
Creeper9207 #7
Posted 20 May 2015 - 02:56 AM
what do you mean? it should work fine
KingofGamesYami #8
Posted 20 May 2015 - 03:05 AM
Oops, I was looking at the wrong function. Indenting your code properly would really help :P/>. If the forums are misrepresenting your indentations, toggle the rich text editor to off (upper left) and manually add [code][/code] tags.
Creeper9207 #9
Posted 20 May 2015 - 03:11 AM
Am i the only one that sees it as indented on the forums??!!??!

Its indented properly for the most part! for me atleast!

Also, i did manually add the code tags
KingofGamesYami #10
Posted 20 May 2015 - 03:12 AM
Yeah, it's mostly indented. Some of it is screwed up though, does it print 'hi' before erroring? I think the line immediately after it is causing trouble.

If it isn't, post the file it's reading from. That way, I can test the script myself (and try to solve the problem).
Lyqyd #11
Posted 20 May 2015 - 03:14 AM
Threads merged. Please stick to one topic for all questions about a given program.
Creeper9207 #12
Posted 20 May 2015 - 03:17 AM
Nice merge, ill make sure to keep this in mind next time i have a new problem

yes it prints hi on line 105 before erroring
Here's the script in the file it reads:
box=2 2 5 5 purple
Bomb Bloke #13
Posted 20 May 2015 - 04:01 AM
Stop being so impatient. Bumping threads so often is annoying and rude.

Anyway, what's this supposed to be doing?

  tonumber(c0)
  tonumber(c1)
  tonumber(c2)
  tonumber(c3)

(That's a rhetorical question - I'm sure you get the idea.)

It sounds like you're falling into the trap of thinking that your code should work and the Lua interpreter is wrong. Such thoughts are a waste of time. You'd've been better off getting your script to print out the exact contents and types of the variables you were passing to the paintutils.drawFilledBox() function, then backtracking through your code to see exactly why they were as they were.
Creeper9207 #14
Posted 20 May 2015 - 04:04 AM
hm, it is a string appearantly, but why didnt tonumber correct it?
i didnt think of printing the type, i forgot it was possible

//sorry, i get bored &amp; spazmatic very easily when i'm lonely
Edited on 20 May 2015 - 02:08 AM
Creeper9207 #15
Posted 20 May 2015 - 04:21 AM
Thanks for the Help, I figured it out :)/>