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

[Question] How Do We Use Table.insert And Table.remove Correctly?

Started by crackroach, 25 August 2013 - 04:32 PM
crackroach #1
Posted 25 August 2013 - 06:32 PM
So, the title explain it i guess, but here's why i came with this question: I am currently making a touchscreen password(yup another <_</>).


--That's just for information


key = {}
  key.digit = {}
	key.digit[1] = {1; x; y}
	key.digit[2] = {2; (x * 2); y}
	key.digit[3] = {3; (x * 3); y}
	key.digit[4] = {4; x; (y * 2)}
	key.digit[5] = {5; (x * 2); (y * 2)}
	key.digit[6] = {6; (x * 3); (y * 2)}
	key.digit[7] = {7; x; (y * 3)}
	key.digit[8] = {8; (x * 2); (y * 3)}
	key.digit[9] = {9; (x * 3); (y * 3)}


-- Check for the mouse click position
local function checkPosition(btn, nmb)
  b = 1
  for a = 1, 9 do
	if mouseW == key.digit[b][2] and mouseH == key.digit[b][3] and btn == 1 then
	  mon.write("key pressed"..key.digit[b][1])
	  table.insert(password, key.digit[b][1])
	  mon.write("pass :"..tostring(password[nmb]))
   end
  b = b + 1
end
end

I am using it correctly?

If you need the rest of the code just tell me and i'll post it

Thx Crackroach
Bubba #2
Posted 25 August 2013 - 09:53 PM
Reverse the variables in the function. The table you want to insert into is the first argument and the thing you're inserting is the second argument.


local sometable = {}
table.insert(sometable, "Index1")
table.remove(sometable, 1)
print(#sometable) --# Outputs 0
Kilobyte #3
Posted 26 August 2013 - 12:47 PM
You can also specify an index to third argument. Then it will insert at that index and not the end.
crackroach #4
Posted 29 August 2013 - 04:34 PM
Ok thanks to both of you.
PixelToast #5
Posted 29 August 2013 - 05:17 PM

key={
digit={
{1,x,y},
{2,x*2,y},
{3,x*3,y},
{4,x,y*2},
{5,x*2,y*2},
{6,x*3,y*2},
{7,x,y*3},
{8,x*2,y*3},
{9,x*3,y*3},
}
}
you might also want to form tables like this