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

advanced table manipulation help

Started by jadelade, 30 April 2013 - 03:54 PM
jadelade #1
Posted 30 April 2013 - 05:54 PM
hi,
im working on a game that requires the player to modify a table by adding boxes to it and the character will fallow the instructions given by the player like a turtle (not a computercraft specific turtle).

here is the table
(there are more rows but i only put in two because it gets a bit big)



local programs = {
  {
  objects = {"none","none","none","none","none","none","none","none","none","none"}
  values = {0,0,0,0,0,0,0,0,0,0}
  },
  {
  objects = {"none","none","none","none","none","none","none","none","none","none"}
  values = {0,0,0,0,0,0,0,0,0,0}
  }
}

i would like to know how to create a for loop that runs thru first row and only the objects table
Kingdaro #2
Posted 30 April 2013 - 06:01 PM
Can be easily done with a numeric loop :D/>/>


-- programs: the programs table
-- programs[1]: the first row of the programs table
-- programs[1].objects: the objects subtable of the first row of the programs table
 -- programs[1].objects[i]: the current object in iteration of the objects subtable of the first row of the programs table

for i=1, #programs[1].objects do
  print(programs[i].objects[i])
end

The pairs loop is also an option - isn't as fast, but is more convenient and requires less typing.


for objNum, object in pairs(programs[i].objects) do
  print(object)
end
jadelade #3
Posted 30 April 2013 - 06:05 PM
Can be easily done with a numeric loop :D/>/>


-- programs: the programs table
-- programs[1]: the first row of the programs table
-- programs[1].objects: the objects subtable of the first row of the programs table
-- programs[1].objects[i]: the current object in iteration of the objects subtable of the first row of the programs table

for i=1, #programs[1].objects do
  print(programs[i].objects[i])
end

The pairs loop is also an option - isn't as fast, but is more convenient and requires less typing.


for objNum, object in pairs(programs[i].objects) do
  print(object)
end

I tried a numeric loop like that but i think i messed up the syntax somewhere
but thanks