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

Moving along contents in an array

Started by TcG, 29 July 2013 - 01:38 PM
TcG #1
Posted 29 July 2013 - 03:38 PM
Moving along contents in an array

This is the code (Explanation and problem below):


m = peripheral.wrap("top")
punkte = {}
arrayPunkt = 0

function body()
  m.clear()
  m.setTextColor(colors.white)
  m.setTextScale(1)
  m.setCursorPos(1,1)
  m.write("-------------------------------------------------------------")
  m.setCursorPos(1, 26)
  m.write("-------------------------------------------------------------")

  m.setTextColor(colors.yellow)
  m.setCursorPos(16, 1)
  m.write("The holy ToDo-List")

end

function setPunkte()
  tiefe = 3
  for i = 0, table.getn(punkte) do
  print (1)
	  m.setCursorPos(2, tiefe)
	  m.setTextColor(colors.lime)
	  m.write("- ")
	  m.setTextColor(colors.orange)
	  m.write(punkte[i])
	  tiefe = tiefe + 2
  end  
end

function add()
  print ("Eingabe: ")
  eingabe = read()
  punkte[arrayPunkt] = eingabe
  arrayPunkt = arrayPunkt + 1
end

function remove()
zeile = 3
m.setTextColor(colors.cyan)
   for i = 0, table.getn(punkte) do
	 m.setCursorPos(47, zeile)
	 m.write("("..(i+1)..")")
	 zeile = zeile + 2
   end
print ("Numbers have been generated on the screen.")
print (Choose the numbers which you want to be gone:")
number = tonumber(read())

end	
while true do

  term.setTextColor(colors.orange)
  write ("Command: 'add' | 'remove' (WIP): ")
  term.setTextColor(colors.white)
  com = read()
  if com == "add" then
	add()
  elseif com == "remove" then
	remove()
  else
   print ("Unknown command.")  
  end
  body()
  setPunkte()
end

A short programm explanation:
It's a programm where you can write things in it which you'd like to in the future (ToDo-List).
There is a little menu where you choose between 'Add' and 'Remove'
By typing 'add' you can add a new line.
Several of them will look like this example:


The numbers behind the lines only appear when you type 'remove' in the menu.

And here's my problem:
I want that if you type in 'remove' you can choose the sentence (-> number) which should be removed.
Then, the sentence will disappear and sentences which were below move along one line so no free space is there anymore.
I'm saving the sentences in a array, so how do I manage that?
Thanks.
Qix #2
Posted 30 July 2013 - 06:22 AM
Look up table.insert() and table.remove(). Instead of setting their values to nil, which causes issues when counting the table's elements, it shifts the elements within the table itself. All you have to know is the index at which to remove.

For a quick-tip, table.insert() with no index simply 'pops' it on to the end of the table (t[#t+1]=v)
jesusthekiller #3
Posted 30 July 2013 - 06:52 AM
Read this about implementing FIFO and LIFO stacks in Lua
Qix #4
Posted 30 July 2013 - 07:06 AM
Read this about implementing FIFO and LIFO stacks in Lua

+1

Note that in some versions of Lua, table.remove() requires an index (though I believe CC uses a version of LuaJ that is high enough that it might not). Fair warning!
jesusthekiller #5
Posted 30 July 2013 - 07:35 AM
Lua In CC does not require arguments :)/>
TcG #6
Posted 30 July 2013 - 11:12 AM
Thanks for the answers! I tried some codes for myself which worked as well, but FIFO is the most compact version.
But now I've got another problem with the program (I hope it will fit in this post, but since it is about my programm I think it's fine.):
I can add lines easily and remove them after that, all no problem. But if I remove a line I'm not able anymore to add another one, because the programm crashs (without an error).

Here's the code (Contains a little bit German words and 'Debug-prints', but that should'nt disturb because you can ignore it and also I'll do a little cleanup later ;)/> )
http://pastebin.com/g0f5mzad