Jump to content
Sign in to follow this  
Jones

The command "for [xxx] in pairs, do"

Recommended Posts

essentially, lua tables save the index and the value.

 

eg array["index"] = value

 

Item[27] = 855

Item["new"] = "hello"

 

eg

for i,v in pairs(Item) do

print(i.." "..v)

end

 

Would output

 

27 855

new hello

  • Like 1

Share this post


Link to post
Share on other sites

for is one of three traditional loops (for, do, while)
why use pairs instead of numeric loop (for i = 0, 5 do {...})?
Arrays in LUA are tables that are dynamic in terms of data structures. (lists, dictionary, arrays, etc.)
Example: t[1] = 5; t['one'] = 5; -- in C, t['one'] is not a valid usage of arrays.
we start using keyword 'pairs' when the indexes are not numerical and want to iterate through a table.

Edited by xtc
  • Like 1

kong.png

a2.png

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...