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

Looping Through A Table But Skipping The First Variable

Started by campicus, 15 October 2013 - 06:53 PM
campicus #1
Posted 15 October 2013 - 08:53 PM
Firstly, I am very new to using ipairs and tables in general, hopefully this is an easy question.

I have this table:


t = {[1]=1,[2]=2,[3]=3,}

I want to run this loop:


for i,v in ipairs(t) do
  print(t[i])
end

How can I run that loop without calling t[1]? basically, run the loop for every value in the table except the first one.

FYI: I did do this but it didn't work:


tArgs = {[1]=1,[2]="right",[3]="left"}

duration = tArgs[1]

sides = tArgs
table.remove(sides, 1)

but this changed the "tArgs" table as well :s
Lyqyd #2
Posted 15 October 2013 - 08:58 PM
Since the tables you're working with are numeric, all you have to do is skip the first entry:


for i, v in ipairs(t) do
  if i > 1 then
    --actual loop body
  end
end
Yevano #3
Posted 15 October 2013 - 09:23 PM
FYI: I did do this but it didn't work:


tArgs = {[1]=1,[2]="right",[3]="left"}

duration = tArgs[1]

sides = tArgs
table.remove(sides, 1)

but this changed the "tArgs" table as well :s

tArgs and sides refer to the same table. You're expecting them to copy the table. If you do have a reason to copy them, then you could run a loop like:


local tArgs = {[1]=1,[2]="right",[3]="left"}
local duration = tArgs[1]
local sides = { }

for i = 2, #tArgs do
	sides[i - 1] = tArgs[i]
end

This will copy all but the first element.
campicus #4
Posted 15 October 2013 - 09:26 PM
Thanks Lyqyd! That worked well (and was super easy). I now have another problem, that my "pulse" function is not looping. The status does not change from "on" to "off". ideas?
EDIT: Cheers Yevano, I thought that may have been the case (dammit lol)
Spoiler

local tArgs = {...}
local program = shell.getRunningProgram() --"this will find the program's path no matter where you saved it"
local status = nil
local duration = nil
local sInterval = nil

os.setComputerLabel("pulser")

if fs.exists("pulserLog") then --check for existing vars
	h = fs.open("pulserLog", "r")
	tArgs = textutils.unserialize(h.readAll())
	h.close()

	duration = tArgs[1]
	sInterval = tonumber(duration)/2

  else --no existing vars, set them
	if #tArgs < 2 or #tArgs > 7 then
	print("Set parameters: <pulse interval> <side1> <side2> ...")
	return
  end

  duration = tArgs[1]
  sInterval = tonumber(duration)/2

  h = fs.open("pulserLog", "w") --log the vars
  h.write(textutils.serialize(tArgs))
  h.close()

  h = fs.open("startup", "w")
  h.write("shell.run(\""..program.."\")")
  h.close()
end

function text()
  term.clear()
  term.setCursorPos(1,1)
  print("Pulsing these sides:\n")
  for i,v in ipairs(tArgs) do
	if i > 1 then
	  print(tArgs[i]..", "..status)
	end
  end
  print("\nevery "..duration.." seconds.")
end

function pulse()
  status = "on"
  for i,v in ipairs(tArgs) do
	if i > 1 then
	  rs.setOutput(tArgs[i], true)
	end
  end
  text()
  sleep(sInterval)
  status = "off"
  for i,v in ipairs(tArgs) do
	if i > 1 then
	  rs.setOutput(tArgs[i], false)
	end
  end
  sleep(sInterval)
  text()
end

while true do
  pulse()
end