Something about Lua tables

Can someone explain me how 'pairs' work in lua?
There is a good tutorial in the Lua's website, but I'm a bit lost.

Code:
revDays = {}
    for i,v in ipairs(days) do
      revDays[v] = i
    end

I get that "revDays = {}" means an empty table, and that this code creates entries for each line/value/expression/block/variable in the 'days' table, but what 'v', 'i' and 'ipairs' do? Why '[v]' between '[]'?
Seems like I'm missing something small, a detail, but very important to grasp what is happening here.
 
You have it backwards, ipairs and pairs are functions you use to iterate over the contents of the table once it has values, not something you use to set values.

ipairs() will give you all consecutive entries with an integer key starting from 1
pairs() will give you all entries (in an indeterminate order)

Code:
local fruit = {}
fruit[1] = "apple"
fruit[3] = "pear"
fruit[2] = "orange"
fruit[0] = "kiwi"
fruit[5] = "banana"
fruit["red"] = "raspberry"

print("By ipairs()")
for i, v in ipairs(fruit) do
  print(i, v)
end

print("By pairs()")
for i, v in pairs(fruit) do
  print(i, v)
end

will display

Code:
By ipairs()
1	apple
2	orange
3	pear
By pairs()
1	apple
2	orange
3	pear
0	kiwi
5	banana
red	raspberry
 
Well, apparently I was missing a bit more than expected. :P
I understand the difference now, thanks.

About what I posted... Sorry, I left a detail out:
There is a table called 'days' with this content:

Code:
 days = {"Sunday", "Monday", "Tuesday", "Wednesday",
            "Thursday", "Friday", "Saturday"}

That line of code that I pasted supposedly created this new table:

Code:
 revDays = {["Sunday"] = 1, ["Monday"] = 2,
                ["Tuesday"] = 3, ["Wednesday"] = 4,
                ["Thursday"] = 5, ["Friday"] = 6,
                ["Saturday"] = 7}
 
That's a fairly key line!

What the code is doing is building an inverse (or reversed) table.

If you have a "day number" (eg 2) days[2] will give you "Monday", but what if you have "Monday" and need to know it's day number?

Once the code has been executed you can now do revDays["Monday"] to get the value 2


Why is this useful?

Code:
local playerIdByCivType = {}

for iPlayer = 0, GameDefines.MAX_MAJOR_CIVS-1, 1 do
  local pPlayer = Players[iPlayer]
  if (pPlayer:IsEverAlive()) then
    playerIdByCivType[GameInfo.Civilizations[pPlayer:GetCivilizationType].Type] = iPlayer
  end
end

You can then use playerIdByCivType["CIVILIZATION_ENGLAND"] to get the player ID for the English player, or nil if they are not in the game
 
There are exactly two kinds of "for loops":

Code:
[B]for [/B]iteratorVariable [B]=[/B] iStart[B],[/B] iStop [, iStep] [B]do[/B]
and
Code:
[B]for [/B]iteratorVariable1 [, iteratorVariable2, ...] [B]in [/B]function([args]) [B]do [/B]
Brackets [] indicate optional stuff. So, for example, iStep in the first for statement is assumed to be 1 if you don't supply it.

pairs and ipairs are both functions (as is anything in Lua followed by parentheses*), so these are an example of the second kind of for loop above. The particular [args] and the number of iteratorVariables depends on the function. pairs and ipairs both require a single table as arg and give you two iteratorVariables (table key and table value). But these aren't the only functions used in the second kind of for loop:

Code:
for pAreaPlot in PlotAreaSpiralIterator(pPlot, r, sector, anticlock, inwards, centre) do
The function here is PlotAreaSpiralIterator and was created by whoward69 (you have to add his plot iterator functions for it to work). It's clearly an example of the 2nd kind of for loop. It uses 6 args and gives you one iterator variable (a plot object).

Code:
for unitInfo in GameInfo.Units() do
Another example of the 2nd type of for loop. In this one, the iterator variable will be a different table each iteration that contains all the info from one row in the table Units.

*GameInfo.Units has type="userdata" rather than "function", but that's rather technical. It still acts like a function in the example above.
 
Back
Top Bottom