For Loop on Lua

My purpose is how to make a for loop. I understood this in terms of numbers, but I cannot understand it in terms of names. I would like to create a for loop that starts a list of names. The following is what I still have:

names = {'John', 'Joe', 'Steve'} for names = 1, 3 do print (names) end 

I tried other things, but it just doesn't work, the terminal always just lists 1, 2, 3 ... What am I doing wrong?

+55
loops lua for-loop
Sep 30 '11 at 21:24
source share
3 answers

Your problem is simple:

 names = {'John', 'Joe', 'Steve'} for names = 1, 3 do print (names) end 

This code first declares a global variable called names . Then you start the for loop. The for loop declares a local variable, also called so-called names ; the fact that a variable was previously defined using names is completely irrelevant. Any use of names inside a for loop will refer to local, not global.

The for loop states that the inside of the loop will be called using names = 1 , then names = 2 and finally names = 3 . The for loop declares a counter that counts from the first number to the last, and it will call the internal code once for each value that it counts.

What you really wanted was something like this:

 names = {'John', 'Joe', 'Steve'} for nameCount = 1, 3 do print (names[nameCount]) end 

The syntax [] is how you access the elements of the Lua table. Lua tables map keys to values. Your array automatically creates integer keys that increase. Thus, the key associated with "Joe" in the table is 2 (Lua indexes always begin with 1).

Therefore, you need a for loop that calculates from 1 to 3, which you get. You use the count variable to access an element from the table.

However, this has a drawback. What happens if you remove one of the items from the list?

 names = {'John', 'Joe'} for nameCount = 1, 3 do print (names[nameCount]) end 

Now we get John Joe nil , as trying to access values ​​from a table that does not exist results in nil . To avoid this, we need to count from 1 to the length of the table:

 names = {'John', 'Joe'} for nameCount = 1, #names do print (names[nameCount]) end 

# is the length operator. It works with tables and rows, returning the length. Now, no matter how big or small names , this will always work.

However, there is a more convenient way to iterate over an array of elements:

 names = {'John', 'Joe', 'Steve'} for i, name in ipairs(names) do print (name) end 

ipairs is a standard Lua function that iterates through a list. This style of the for loop, an iterator for a loop, uses such an iterator function. The value i is the index of the record in the array. The value of name is the value of this index. So it basically does a lot of work for you.

+141
01 Oct 2018-11-11T00: 00Z
source share

Reading online (a guide to tables ), it seems that tables behave like arrays, so you are looking for:

Way1

 names = {'John', 'Joe', 'Steve'} for i = 1,3 do print( names[i] ) end 

Way2

 names = {'John', 'Joe', 'Steve'} for k,v in pairs(names) do print(v) end 



Way1 uses the index/key table, in your names table each element has a key starting with 1, for example:

 names = {'John', 'Joe', 'Steve'} print( names[1] ) -- prints John 

So you just make i go from 1 to 3.

In Way2, instead, you specify which table you want to run, and assign a variable to its key and value, for example:

 names = {'John', 'Joe', myKey="myValue" } for k,v in pairs(names) do print(k,v) end 

prints the following:

 1 John 2 Joe myKey myValue 
+11
30 Sep '11 at 21:52
source share
 names = {'John', 'Joe', 'Steve'} for names = 1, 3 do print (names) end 
  • You delete your table and replace it with int
  • You do not pull the value from the table

Try:

 names = {'John','Joe','Steve'} for i = 1,3 do print(names[i]) end 
-one
Jan 01 '15 at 19:11
source share



All Articles