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

Arrays and for loops

Started by Mineorbit, 23 April 2014 - 08:01 AM
Mineorbit #1
Posted 23 April 2014 - 10:01 AM
Hey folks,

How can I program an array or better, can I turn an table to an array?

And then, is there a possibility to let this work with a for loop?

In java there is a posibility like this


List<Object> objectarray = new ArrayList<Object>();
for(Object o : objectarray)
{
System.out.println(o);
}
How could I do this with lua(computercraft?

Thank you!

Mineorbit
Lyqyd #2
Posted 23 April 2014 - 03:31 PM
Tables can be used as arrays. They're one-indexed, and all the table library functions expect this format. You can iterate one with a numeric for loop, or you can use a generic for loop with the ipairs iterator (pairs can be used with array-style tables and tables with arbitrary keys as well, ipairs can only be (successfully) used with sequential numerically indexed tables).


for key, value in ipairs(myTable) do
  print(value)
end