Generic for

From Legacy Roblox Wiki
Revision as of 10:45, 13 July 2011 by >Flurite (→‎ipairs and pairs)
Jump to navigationJump to search

Introduction

Basic for loops have already been covered in Loops. If you haven't already done so, please review Loops.

Discussion

The generic for loop traverses all values returned by the iterator. The iterator is the number that keeps track of how many times a for loop is supposed to run. This is different from a the numeric for loop in Loops in that before we were simply displaying the iterator (or a function of the iterator) itself:

Numeric for:

for i=20, 1, -2 do print(i) end

Now what we are doing is assigning a value to the iterator.

Let's make Table with the months of the year:

months = {"January", "February", "March", "April", "May", "June", "July",
 "August", "September", "October", "November", "December"}

We know that there are twelve months in the year, and we can also call January "month #1", February "month #2", and so on.:

revmonths = {["January"] = 1, ["February"] = 2, ["March"] = 3, ["April"] = 4, ["May"] = 5, ["June"] = 6,
 ["July"] = 7, ["August"] = 8, ["September"] = 9, ["October"] = 10, ["November"] = 11, ["December"] = 12}

Notice how we assigned a unique month number to each month of the year. Now we can find our iterators again:

print(revmonths["January"]) -- prints 1, which is the first month of the year
print(revmonths["December"]) -- prints 12, which is the twelfth month of the year

Script

months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", 
"November", "December"}

revmonths = {["January"] = 1, ["February"] = 2, ["March"] = 3, ["April"] = 4, ["May"] = 5, ["June"] = 6, 
["July"] = 7, ["August"] = 8, ["September"] = 9, ["October"] = 10, ["November"] = 11, ["December"] = 12}

print(revmonths["January"])
print(revmonths["February"])
print(revmonths["March"])
print(revmonths["April"])
print(revmonths["May"])
print(revmonths["June"])
print(revmonths["July"])
print(revmonths["August"])
print(revmonths["September"])
print(revmonths["October"])
print(revmonths["November"])
print(revmonths["December"])

Instead of having to slough through typing a print() command for every month, you can now do a for loop:

months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October",
 "November", "December"}

revmonths = {}
    
for i, v in ipairs(months) do
   revmonths[v] = i
   print(revmonths[v])
end

ipairs and pairs

Usually when people see ipairs they get confused because pairs is more common. What ipairs does is search through all numerical indexes. Two examples below, one will work and one won't.

Below will not work because there is no numerical index, we created a new index and named is "Hello". When you don't create indexes, they are automatically numbers in numerical order.


local Table = {["Hello"] = "HelloIsANewIndex"}

for i,v in ipairs(Table) do
  print(i,v)
end

Below will work because we used pairs. pairs goes through all indexes, non numerical or numerical.


local Table = {["Hello"] = "HelloIsANewIndex", ["Apple"] = "Green"}

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

In this case, when the 'i' variable is "Hello", the 'v' variable is going to be "HelloIsANewIndex". When the 'v' variable is "Apple", the 'v' variable is going to be "Green".


>Hello HellowIsANewIndex

Here is a working example of using ipairs.

table = {"1", "Hi", "55", "Banana", "Phone"}

for i, v in ipairs(table) do
   print(i, v)
end

When using ipairs, the 'i' variable is the position of the key, and the 'v' variable is the key. So when 'i' is 1, then 'v' would be "1", when 'i' is 2, then 'v' will be "Hi". See the difference between ipairs and pairs?