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

[Lua - Question]

Started by JoshhT, 09 October 2012 - 08:32 AM
JoshhT #1
Posted 09 October 2012 - 10:32 AM
So I was just browsing around the forums here when I noticed this code.
(I've cleaned and focused it down to what I'm asking.)

local x = {1, 2, 3, 4, 5, 6}

for i, v in pairs(x) do
  print(v)
end

Can someone please explain exactly what "v in pairs(arg)" does?
And how it varies from, say.

v = x.n
for i, v do
  print(i)
end

Please note: I consider myself quite a good programmer, with several years experience in Java.
So I know my way around efficient coding and complex programs.
I also know I could just google this, but I figure, why not say hello to the CC community and meet a few people.

Cheers guys :D/>/>
robhol #2
Posted 09 October 2012 - 10:52 AM
pairs is an iterator. for k,v in pairs(tbl) is the same as iterating over keys and values from a Dictionary. In this case, i==v for all iterations. Note that array indices are 1-based unlike pretty much every other language.
JoshhT #3
Posted 09 October 2012 - 10:59 AM
pairs is an iterator. for k,v in pairs(tbl) is the same as iterating over keys and values from a Dictionary. In this case, i==v for all iterations. Note that array indices are 1-based unlike pretty much every other language.
Well that was simple enough. Lol.
Thanks mate.