Jump to content




[CC]Code Cleanup


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

#141 KingofGamesYami

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

Posted 02 March 2015 - 08:33 PM

View PostDog, on 02 March 2015 - 08:26 PM, said:

Good point :)/>
local mon = perihperal.find(monitor)
term.setTextColor(type(mon) == "table" and colors.green or colors.red)
term.write(type(mon) == "table" and "Monitor found" or "No monitor found")
if type(mon) == "table" then mon.write("Hello World!")

local mon = peripheral.find( "monitor" ) --#monitor needs quotes, peripheral was spelled incorrectly
term.setTextColor( type(mon)=="table"and colors.green or colors.red ) --#no changes needed
term.write( type(mon) == "table" and "Monitor found" or "No monitor found" ) --#no changes
if type( mon ) == "table" then mon.write( "Hello World!" ) --#added end

Bugged code:
while not turt1e.forward() do
  turt1e.dig()
  turt1e.attack()
end
print( moved forward! )


#142 Lemmmy

  • Members
  • 218 posts

Posted 02 March 2015 - 08:35 PM

local mon = peripheral.find(monitor)
term.setTextColor(type(mon) == "table" and colors.green or colors.red)
term.write(type(mon) == "table" and "Monitor found" or "No monitor found")
if type(mon) == "table" then mon.write("Hello World!")
end

Edit: Ninja'd :(

while not turtle.forward() do
  turtle.dig()
  turtle.attack()
end
print("moved forward!")

local function draw()
	term.setTextColór(color.white)
	term.selBackgroundColor(color.yellow)
	term.setCursorPos(0, 0)
	term.write("Draw”)
end

draw()

Edited by LemonLake, 02 March 2015 - 08:36 PM.


#143 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

Posted 02 March 2015 - 08:47 PM

View PostLemonLake, on 02 March 2015 - 08:35 PM, said:

...
Edit: Ninja'd :(
...
local function draw()
  term.setTextColór(color.white)
  term.selBackgroundColor(color.yellow)
  term.setCursorPos(0, 0)
  term.write("Draw”)
end

draw()

local function draw()
  term.setTextColor(colors.white) --# setTextColor was spelled with a special character - colors.white not color.white
  term.setBackgroundColor(colors.yellow) --# term.set, not term.sel - colors.yellow, not color.yellow
  term.setCursorPos(1, 1) --# 0,0 is offscreen, should be 1,1
  term.write("Draw") --# used a 'closing quote' after Draw - replaced with standard quote
end

draw() --# no changes

EDIT: found another mistake

Edited by Dog, 02 March 2015 - 08:49 PM.


#144 Lupus590

  • Members
  • 2,028 posts
  • LocationUK

Posted 02 March 2015 - 09:00 PM

once again, your bugged code...

#145 cptdeath58

  • Members
  • 139 posts
  • LocationError 404: Could not find.

Posted 02 March 2015 - 11:17 PM

No code above :/
function pie
term.write(" I like pie"")
end

pue()

Edited by cptdeath58, 02 March 2015 - 11:23 PM.


#146 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

Posted 02 March 2015 - 11:29 PM

View PostLupus590, on 02 March 2015 - 09:00 PM, said:

once again, your bugged code...

Oh my, I'm failing at this...no more playing while working...

View Postcptdeath58, on 02 March 2015 - 11:17 PM, said:

No code above :/
function pie
term.write(" I like pie"")
end

pue()


Corrected code:
[/color]
[color=#880000]local function pie() --# brackets were missing, localized function as a matter or practice
  term.write(" I like pie") --# extra quote at the end, add indentation
end --# no changes

pie()[/color][color=#880000] --# pie, not pue[/color]
[color=#880000]


New code (didn't forget this time :P )
local openFile = fs.open("/disk/data")
local fileContents = fs.readAll()
openFile.close
local newData = textutils.serialize(fileContents)

Edited by Dog, 03 March 2015 - 12:36 AM.


#147 Dragon53535

  • Members
  • 973 posts
  • LocationIn the Matrix

Posted 03 March 2015 - 07:37 AM

View PostDog, on 02 March 2015 - 11:29 PM, said:

--snip--


New code (didn't forget this time :P )
local openFile = fs.open("/disk/data")
local fileContents = fs.readAll()
openFile.close
local newData = textutils.serialize(fileContents)

Corrected:
local openFile = fs.open("/disk/data","r") --#Missing mode, in this case read
local fileContents = openFile.readAll() --#Was fs.readAll, incorrect as it needs the file handle.
openFile.close() --#Missing parenthesis
local newData = textutils.unserialize(fileContents) --#Was serialize, needed to be unserialize
That should be it.



local Totes = 0
for a,v in pairs(color) go
  if type(v) != "function" then
	Totes = totes + v
  end
end
print(Totes + " Numbers")


#148 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

Posted 04 March 2015 - 05:25 AM

View PostDragon53535, on 03 March 2015 - 07:37 AM, said:

...
local Totes = 0
for a,v in pairs(color) go
  if type(v) != "function" then
	Totes = totes + v
  end
end
print(Totes + " Numbers")

local Totes = 0 --# no changes
for a,v in pairs(color) do --# change 'go' to 'do'
  if type(v) ~= "function" then --#change '!=' to '~='
	Totes = Totes + v --# change 'totes + v' to 'Totes + v'
  end
end
print(Totes .. " Numbers") --# change '+' to '..' for string concatenation

I *think* that's everything...

local mon = peripheral.find("monitor")
mon.setBackGroundColor(cyan)
mon.setTextColor(white)
mon.setCursorPos(1, 10)
mon.write("   /\	 \/   ")

Edited by Dog, 04 March 2015 - 05:26 AM.


#149 Grim Reaper

  • Members
  • 503 posts
  • LocationSeattle, WA

Posted 04 March 2015 - 07:19 AM

View PostDog, on 04 March 2015 - 05:25 AM, said:

local mon = peripheral.find("monitor")
mon.setBackGroundColor(cyan)
mon.setTextColor(white)
mon.setCursorPos(1, 10)
mon.write("   /\	 \/   ")
  • You're using "setBackGroundColor" which doesn't exist. It should be "setBackgroundColor."
  • The colors you're referencing need to be preceded by "colors" and a dot-separator.
  • You're using backslashes which require escape sequences. As opposed to "/\," it should be "/\\."

local function putValue (table, index, value)
  table.insert (table, index, value)
end

local data = {1; 2; 3; 4; 5;}
local thread = coroutine.create (putValue)

thread.resume (data, 6, 6)


#150 Dragon53535

  • Members
  • 973 posts
  • LocationIn the Matrix

Posted 04 March 2015 - 01:15 PM

View PostGrim Reaper, on 04 March 2015 - 07:19 AM, said:

local function putValue (table, index, value)
  table.insert (table, index, value)
end

local data = {1; 2; 3; 4; 5;}
local thread = coroutine.create (putValue)

thread.resume (data, 6, 6)

Corrected:
local function putValue (tbl, index, value) --#CANNOT use the variable table
  table.insert (tbl, index, value) --#No table variable for you
end

local data = {1, 2, 3, 4, 5,}--# ; should be ,
local thread = coroutine.create (putValue)

coroutine.resume(thread, data, 6, 6)--#Needed coroutine instead of thread, and thread needed to be first argument
I BELIEVE that's all.

local function digTurtle(num)
for 1, num do
turtle.dig()
end
print("How much do you want to go forward?')
t = read()
digTutle(t)


Ps. Dog, it's the colors api, not the color api. So you missed the s on color. :P

#151 TurtleHunter

  • Members
  • 112 posts

Posted 04 March 2015 - 01:24 PM

View PostGrim Reaper, on 04 March 2015 - 07:19 AM, said:

local function digTurtle(num)
for 1, num do
turtle.dig()
end
print("How much do you want to go forward?')
t = read()
digTutle(t)


Ps. Dog, it's the colors api, not the color api. So you missed the s on color. :P

local function digTurtle(num)
  for i=1, num do
	 turtle.dig()
  end
end
print("How much do you want to go forward?")
t = read()
digTurtle(tonumber(t))

Added end, the value of for must be assigned to a variable, t is string

funtion hello()
  print({..})
end

Edited by TurtleHunter, 04 March 2015 - 01:24 PM.


#152 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

Posted 04 March 2015 - 04:28 PM

View PostTurtleHunter, on 04 March 2015 - 01:24 PM, said:

funtion hello()
  print({..})
end

function hello() --# function not funtion - missing 'c'
  print(textutils.serialize( ... )) --# missed a dot and included table brackets - I'm guessing you are passing a table of command line arguments, so it would need to be serialized or have the values teased out with a loop
end

Dragon - I made the incorrect assumption that it wasn't the colors API and undid my 'correction' of that - d'oh! Oh, and you can use semi-colons instead of commas as separators with Lua - it works :)

local items = { boxes = 7, paper = 100, ink = 25 }
do
  local function getTotalItemCount()
    for i = 1, #items do
      items[i] = items[i] + 1
    end
    return
  end
end
print(getTotalItemCount)

Edited by Dog, 04 March 2015 - 04:30 PM.


#153 Lignum

  • Members
  • 558 posts

Posted 04 March 2015 - 04:33 PM

View PostTurtleHunter, on 04 March 2015 - 01:24 PM, said:

funtion hello()
  print({..})
end

local function hello(...) --# Varargs required as parameter; local not necessary but... I insist
   print({...}) --# Three periods are needed.
end


EDIT: Ninja'd

local items = { boxes = 7, paper = 100, ink = 25 }
--#do
  local function getTotalItemCount()
    --# I'm not sure what the previous code was attempting to do but it's fixed now...
    local totalCount = 0
    for _,v in pairs(items) do
       totalCount = totalCount + v
    end
    return totalCount
  end
--#end Creation of a new chunk prevents getTotalItemCount from being used as an upvalue later.
print(getTotalItemCount()) --# Call instead of passing a function pointer.

local function find(tbl, pred) --# Finds the first element in a table that matches a predicate.
   for _,v in ipairs(tbl) do
	  local result = pred(v)
	  if result then
		 return result
	  end
   end
end

local found = find({ "Hello", "World", "1", "2", "3" }, function(v)
   return tonumber(v) ~= nil --# Find the first string that contains a number.
end)

term.write(found)
print()

For convenience: window:155: bad argument: string expected, got boolean

Edited by Lignum, 04 March 2015 - 04:36 PM.


#154 Lemmmy

  • Members
  • 218 posts

Posted 04 March 2015 - 08:05 PM

local function find(tbl, pred) --# Finds the first element in a table that matches a predicate.
   for _,v in ipairs(tbl) do
		  local result = pred(v)
		  if result then
				 return v --# return v, not result
		  end
   end
end

local found = find({ "Hello", "World", "1", "2", "3" }, function(v)
   return tonumber(v) ~= nil --# Find the first string that contains a number.
end)

term.write(found)
print()
that took me so long....

local function victoryString()
	local victoryString = "I found your damn problem
end

local function found()
	print victoryString
end

found()

Edited by LemonLake, 04 March 2015 - 08:06 PM.


#155 cptdeath58

  • Members
  • 139 posts
  • LocationError 404: Could not find.

Posted 05 March 2015 - 02:21 AM


-- removed local function
local victoryString = "I found your **** problem" -- Added final quotation and censored word. (There are young people who look at that)
-- removed end

local function found() -- no changes
print(victoryString) -- Surrounded "victoryString" in parenthesis/brackets
end --no changes
found() -- no changes

Edited by cptdeath58, 05 March 2015 - 02:52 AM.


#156 Lignum

  • Members
  • 558 posts

Posted 05 March 2015 - 02:32 PM

View Postcptdeath58, on 05 March 2015 - 02:21 AM, said:

...
... your bugged code?

View Posttheoriginalbit, on 29 May 2014 - 03:16 AM, said:

My challenge (20 problems)

After a long time of neglect, I have solved your challenge.
Spoiler


#157 Dragon53535

  • Members
  • 973 posts
  • LocationIn the Matrix

Posted 05 March 2015 - 06:57 PM

View PostLignum, on 05 March 2015 - 02:32 PM, said:

View Postcptdeath58, on 05 March 2015 - 02:21 AM, said:

...
... your bugged code?

...
And yours? :P


for a=1,50 do
  wut(a)
  local function wut(int)
    print(int)
  end
end


#158 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 05 March 2015 - 09:19 PM

View PostLignum, on 05 March 2015 - 02:32 PM, said:

View Postcptdeath58, on 05 March 2015 - 02:21 AM, said:

...
... your bugged code?

View Posttheoriginalbit, on 29 May 2014 - 03:16 AM, said:

My challenge (20 problems)

After a long time of neglect, I have solved your challenge.
Spoiler

I think you changed the code too much here :P. Here's mine:

Spoiler

View PostDragon53535, on 05 March 2015 - 06:57 PM, said:

for a=1,50 do
  wut(a)
  local function wut(int)
    print(int)
  end
end

Here you go:

for a=1,50 do
  local function wut(int)
    print(int)
  end
  wut(a)
end

And here's a small piece of code that needs some fixing:

local table = {1, 2; "string"]

table.insert(1, 0)

for i = 1; #tab do
   print(tab[1]) 
end

local temp

for i = #tab, 1 do
   _temp[#tab+1] = tab[i]
end

tab = _temp

for v, k in pairs(tab) do
   print("key: ", k) 
   print("value: "; v)
end

Edited by MKlegoman357, 05 March 2015 - 09:32 PM.


#159 Lemmmy

  • Members
  • 218 posts

Posted 05 March 2015 - 10:16 PM

local tab = {1, 2, "string"}

table.insert(tab, 1)

for i = 1, #tab do
   print(tab[i])
end

local temp = {}

for i = 1, #tab do
   temp[#temp+1] = tab[i]
end

tab = temp

for k, v in pairs(tab) do
   print("key: " .. k)
   print("value: " .. v)
end

local table = []
table.insert("yo, this is my string")
for v in pairs(table) do
    printer("i got a thing from yo table, and looking at previous code its probably a string so here it is:")
    printer(v)
end


#160 Dragon53535

  • Members
  • 973 posts
  • LocationIn the Matrix

Posted 05 March 2015 - 11:28 PM

local tbl = {} --# Can't use [], and table is an api keyword, don't use it as a variable
table.insert(tbl,"yo, this is my string") --#You need the table's name
for a,v in pairs(tbl) do --#Needs to point to table, and needs two variables
	print("i got a thing from yo table, and looking at previous code its probably a string so here it is:") --#Printer to print
	print(v)--#printer to print
end

If hugs == nil then
  prinl("You dun wunt hugs?")
if Hugs ~= nil then
  print("YUS, HUGGERS(
end


Mklegoman, it's bad practice to create a table in a loop, especially a loop that simple as I made mine. I was hoping you would put the declaration before the loop.

Also, lemon, your table.insert on your correction is wrong.
table.insert(tab,1,0)
Is the correct one, since he wanted the integer 0 at the index of 1

Edited by Dragon53535, 05 March 2015 - 11:29 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users