Jump to content




[Lua - Question]


  • You cannot reply to this topic
2 replies to this topic

#1 JoshhT

  • New Members
  • 64 posts
  • LocationAustralia

Posted 09 October 2012 - 09: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/>

#2 robhol

  • Members
  • 182 posts

Posted 09 October 2012 - 09: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.

#3 JoshhT

  • New Members
  • 64 posts
  • LocationAustralia

Posted 09 October 2012 - 09:59 AM

View Postrobhol, on 09 October 2012 - 09:52 AM, said:

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.





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users