Jump to content




Arrays and for loops


  • You cannot reply to this topic
1 reply to this topic

#1 Mineorbit

  • New Members
  • 1 posts

Posted 23 April 2014 - 09: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

#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 23 April 2014 - 02: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






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users