Jump to content




Help With FOR



  • You cannot reply to this topic
14 replies to this topic

#1 Creeper9207

  • Members
  • 211 posts

Posted 19 May 2015 - 09: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 by Creeper9207, 19 May 2015 - 11:41 PM.


#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 20 May 2015 - 12: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 by Bomb Bloke, 20 May 2015 - 12:48 AM.


#3 Creeper9207

  • Members
  • 211 posts

Posted 20 May 2015 - 12:59 AM

thanks man, everybody in irc was like "format it rite" when it was already correct XD

#4 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 20 May 2015 - 01: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.

#5 Creeper9207

  • Members
  • 211 posts

Posted 20 May 2015 - 01: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 by Creeper9207, 20 May 2015 - 02:09 AM.


#6 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 20 May 2015 - 01: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.

#7 Creeper9207

  • Members
  • 211 posts

Posted 20 May 2015 - 01:56 AM

what do you mean? it should work fine

#8 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 20 May 2015 - 02: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.

#9 Creeper9207

  • Members
  • 211 posts

Posted 20 May 2015 - 02: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

#10 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 20 May 2015 - 02: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).

#11 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 20 May 2015 - 02:14 AM

Threads merged. Please stick to one topic for all questions about a given program.

#12 Creeper9207

  • Members
  • 211 posts

Posted 20 May 2015 - 02: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

#13 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 20 May 2015 - 03: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.

#14 Creeper9207

  • Members
  • 211 posts

Posted 20 May 2015 - 03: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 & spazmatic very easily when i'm lonely

Edited by Creeper9207, 20 May 2015 - 03:08 AM.


#15 Creeper9207

  • Members
  • 211 posts

Posted 20 May 2015 - 03:21 AM

Thanks for the Help, I figured it out :)





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users