Jump to content




[CC]Code Cleanup


170 replies to this topic

#41 SuicidalSTDz

    Permutator of Strings

  • Members
  • 1,308 posts
  • LocationPennsylvania

Posted 07 March 2013 - 06:41 PM

View PostGrim Reaper, on 07 March 2013 - 06:05 PM, said:

I suppose it would count as an error had he specified what the code written was trying to accomplish. in some cases, returning nil may not be an error but just a flag which drives a program in a different direction.
touché :P Although in this case, it was indeed a nil value which was never intended to drive the program any further ;)

#42 Grim Reaper

  • Members
  • 503 posts
  • LocationSeattle, WA

Posted 08 March 2013 - 03:40 AM

View PostSuicidalSTDz, on 07 March 2013 - 06:41 PM, said:

View PostGrim Reaper, on 07 March 2013 - 06:05 PM, said:

I suppose it would count as an error had he specified what the code written was trying to accomplish. in some cases, returning nil may not be an error but just a flag which drives a program in a different direction.
touché :P Although in this case, it was indeed a nil value which was never intended to drive the program any further ;)

I agree :)

#43 Smiley43210

  • Members
  • 204 posts

Posted 18 March 2013 - 01:57 AM

Since nobody did it:

View PostSuicidalSTDz, on 07 March 2013 - 03:55 PM, said:

My turn :P/>/>
-snip-
local open = io.open
local spam = "SPAM!"
local handle = open("test", "w")
local data = {}
for i = 1,100 do
table.insert(data,spam.."THIS IS SPAM!")
end
handle:write(textutils.serialize(data))
handle:close()
local open = io.open("test", "r")
local read = open:read()
local dataTable = textutils.unserialize(read)
open:close()
for k,v in ipairs(dataTable) do
print(v)
end
Missing quotation marks around w and r in open
Missing quotation mark to close string THIS IS SPAM
Missing do closer after both for statements
Wrong type of integrator (ipairs not pairs)
Ehhh....y u no make it simpler D:< ... jk

Mine
NOTE: It's purpose is to set monitorSide to the side a monitor is attached OR "none" if none. Keep that in mind.
local sides = {"left, "right", "top", "bottom", "front" "back}
local monitorSide = "none"

function check(side)
 if peripheral.getType(side) ~= "monitor" then monitorSide = side end
end

for i, v in pairs(sides) then
 check(v)
end


#44 faubiguy

  • Members
  • 213 posts

Posted 26 March 2013 - 08:57 AM

View PostSmiley43210, on 18 March 2013 - 01:57 AM, said:

local sides = {"left, "right", "top", "bottom", "front" "back}

local monitorSide = "none"

function check(side)
if peripheral.getType(side) ~= "monitor" then monitorSide = side end
end

for i, v in pairs(sides) then
check(v)
end
Fixed:
local sides = {"left", "right", "top", "bottom", "front", "back"}

local monitorSide = "none"

function check(side)
if peripheral.getType(side) == "monitor" then monitorSide = side end
end

for i, v in ipairs(sides) then
check(v)
end

- Added closing quotes on "left" and "back"
- Added comma to separate "front" and "back"
- Changed ~= to ==
- Changed pairs to ipairs

My code:
local function() dostuff
  locol numbers = {}
  local words = {one = 1, ten = 10, one hundred = 100, one thousand = 1000)
  for [i = 1 to 1 000] do
	if [i = 10 OR (i < 500 AND i > 300) ] do
	  numbers[i] = Math:random(1, i)
-[[
	else
	  numbers[i] = -i
	end
]]
  end
  local number = 0
  while [number < 1000] do
	for k,v in ipairs(words) do
	  if [numbers[number] = v] do
		term.print k
	  end
	end
  end
  return number[10] + numbers[400]
end


#45 yaaay

  • Banned
  • 122 posts

Posted 27 March 2013 - 01:43 AM

term.write(Tro)
while true do
term.write(lo)
end
__ This is something annoying
--{{
Haha
}}--
lololol
hehehe
blablabla
wowowo
--[[ Madness! ]]--

#46 dan200

  • Administrators
  • 542 posts
  • LocationCambridge, England

Posted 28 March 2013 - 05:45 AM

local monitor=nil
for i,side in ipairs( peripheral.getNames() ) do
    if peripheral.getType( side ) == "monitor" then
        monitor = side
        break
    end
end


#47 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 28 March 2013 - 02:47 PM

Ah, sneaky dan, using peripheral.getNames() instead of getTypes(). I see what you did.

#48 Dlcruz129

    What's a Lua?

  • Members
  • 1,423 posts

Posted 28 March 2013 - 05:31 PM

View PostCranium, on 28 March 2013 - 02:47 PM, said:

Ah, sneaky dan, using peripheral.getNames() instead of getTypes(). I see what you did.

Incorrect! It should be rs.getSides(), as he is making an auto-monitor wrapping program.

Omg I corrected a mod. I r smrt.

#49 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 29 March 2013 - 02:08 AM

Herp derp....He did use getType().....
Meh, I was tired.

#50 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 29 March 2013 - 02:17 AM

View PostDlcruz129, on 28 March 2013 - 05:31 PM, said:

Omg I corrected a mod. I r smrt.
Mods dont have to be great at coding. they just have to be good at moderating. now if you corrected dan200 or cloudy and it wasn't just a simple typo. then thats different.

#51 Dlcruz129

    What's a Lua?

  • Members
  • 1,423 posts

Posted 29 March 2013 - 12:03 PM

View PostTheOriginalBIT, on 29 March 2013 - 02:17 AM, said:

View PostDlcruz129, on 28 March 2013 - 05:31 PM, said:

Omg I corrected a mod. I r smrt.
Mods dont have to be great at coding. they just have to be good at moderating. now if you corrected dan200 or cloudy and it wasn't just a simple typo. then thats different.

:( y u rain on parade?

#52 Spongy141

  • Members
  • 526 posts
  • Location'Merica

Posted 30 March 2013 - 07:20 PM

What if there was no error in the code? :blink:

#53 Dlcruz129

    What's a Lua?

  • Members
  • 1,423 posts

Posted 31 March 2013 - 02:49 AM

View PostSpongy141, on 30 March 2013 - 07:20 PM, said:

What if there was no error in the code? :blink:/>

He iterated through peripheral.getNames() instead of rs.getSides()

#54 diegodan1893

  • Members
  • 164 posts
  • LocationSpain

Posted 06 April 2013 - 06:46 AM

This one is difficult:

local testString = "This is a sample string";
local testSubString = string.sub(testString,5);
local indexOfSample = string.find(testSubString,"sample");
print(indexOfSample)


#55 cant_delete_account

  • Members
  • 484 posts

Posted 06 April 2013 - 07:06 AM

View Postdiegodan1893, on 06 April 2013 - 06:46 AM, said:

This one is difficult:

local testString = "This is a sample string";
local testSubString = string.sub(testString,5);
local indexOfSample = string.find(testSubString,"sample");
print(indexOfSample)
Works fine? Prints out 7. Is it supposed to do something else? (I'm not so familiar with string.find)
Only other thing I could think of was this:
local testString = "This is a sample string";
local s,e = string.find(testString,"sample");
print(testString:sub(s, e))
Or this:
local testString = "This is a sample string";
local testSubString = string.sub(testString,5)
local s,e = string.find(testSubString,"sample");
print(testString:sub(s, e))


#56 diegodan1893

  • Members
  • 164 posts
  • LocationSpain

Posted 06 April 2013 - 07:13 AM

View Postthesbros, on 06 April 2013 - 07:06 AM, said:

View Postdiegodan1893, on 06 April 2013 - 06:46 AM, said:

This one is difficult:

local testString = "This is a sample string";
local testSubString = string.sub(testString,5);
local indexOfSample = string.find(testSubString,"sample");
print(indexOfSample)
Works fine? Prints out 7. Is it supposed to do something else? (I'm not so familiar with string.find)
Only other thing I could think of was this:
local testString = "This is a sample string";
local s,e = string.find(testString,"sample");
print(testString:sub(s, e))
Run it in computercraft, it prints 11. It's a bug in CC. It's easy to fix it, but I don't want to reveal the answer.

#57 cant_delete_account

  • Members
  • 484 posts

Posted 06 April 2013 - 07:15 AM

View Postdiegodan1893, on 06 April 2013 - 07:13 AM, said:

View Postthesbros, on 06 April 2013 - 07:06 AM, said:

View Postdiegodan1893, on 06 April 2013 - 06:46 AM, said:

This one is difficult:

local testString = "This is a sample string";
local testSubString = string.sub(testString,5);
local indexOfSample = string.find(testSubString,"sample");
print(indexOfSample)
-snip-
Run it in computercraft, it prints 11. It's a bug in CC. It's easy to fix it, but I don't want to reveal the answer.
Ah forgot about that one. Here then:
local testString = "This is a sample string";
local testSubString = string.sub(testString,5);
local indexOfSample = string.find(testSubString.."","sample");
print(indexOfSample)
(can't test it since I don't have access to Minecraft right now)

#58 Apfeldstrudel

  • Members
  • 161 posts

Posted 04 July 2013 - 09:18 AM

No error code? Wellwell, here goes
counterVar == 0
while true do
  countervar = counterVar+1
  os.PullEvent("Redstone")
  For i = 1,counterVar
    For j = 1, i
      term.write("*')
    end
    Term.write("\newline");
   end
 term.write("\newline")


#59 M4sh3dP0t4t03

  • Members
  • 255 posts
  • LocationGermany

Posted 04 July 2013 - 12:47 PM

View PostXyexs, on 04 July 2013 - 09:18 AM, said:

No error code? Wellwell, here goes
counterVar == 0
while true do
  countervar = counterVar+1
  os.PullEvent("Redstone")
  For i = 1,counterVar
	For j = 1, i
	  term.write("*')
	end
	Term.write("\newline");
   end
term.write("\newline")
counterVar = 0
while true do
  counterVar = counterVar+1
  os.pullEvent("redstone")
  for i = 1,counterVar do
    for j = 1, i do
	  write("*")
	end
    write("\n");
   end
write("\n")
end
- == instead of = in first line
- some fixes because Lua is case-sensitive
- there were no do's after the for statements
- you use " to open a string and ' to close it
- "print("\newline")" would print "
ewline"
- there was a missing end at the end
Edit: I missed that term.write() can't print \n

#60 MudkipTheEpic

  • Members
  • 639 posts
  • LocationWhere you'd least expect it.

Posted 04 July 2013 - 04:19 PM

term.write doesnt accept \n's. Its write that parses those.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users