For loops and questions.

Hey,
I’m not too good with for loops but understanding them is easy enough. My main question is what is the difference between:
[lua]for k, v in ipairs () do[/lua]
and:
[lua]for k, v in pairs () do[/lua]

Does the I make anything significant? Also what does the _ in:
[lua]for _, v in pairs[/lua]
precisely do? Is it just another character used for a variable or does it have its own way of changing the for loop?

one question btw… what makes the k,v ?

Not sure if it has to be k,v; But if you had 3 items in a table you’d have something like this:
[lua]
bob = {}
bob[1] = {STATEMENT= “Hello”}
bob[2] = {STATEMENT= “I am”}
bob[3] = {STATEMENT= “Kalik? No, never mind.”}

–Then you can do something like…:
for k,v in pairs(bob)
Msg(v.STATEMENT)
end
–I’m not entirely sure if that will completely work, but I can’t think of anything.
– k would first be equal to 1 (so bob[k] or bob[1]) and it would print the STATEMENT of bob[1]
–Then it would do bob[2] statement, etc.
[/lua]

As for the initial question, I have absolutely no idea, a quick guess at it:

[editline]01:42PM[/editline]

[lua]for k, v in ipairs () do[/lua]
This one I have absolutely no idea about, even though I’ve seen it, I have no clue.
Here’s something I found in fortwars lite:
[lua]
for i,v in ipairs(TEAMCOLOR) do
if ply:Team() == i then
ply:Fire(“color”,v,0)
end
end
[/lua]

[lua]for k, v in pairs () do[/lua]
This is one is the most common one and follows the example above.

[lua]for _, v in pairs[/lua]
For this one: Quick Guess( I’ve used it, but I can’t quite recall.) I believe if you have a check, or something that isnt specified, for example
(for _,t in pairs(ents.FindByClass(“The_Flag”) do…)
because you dont have a preset thing, but it could be there and there could be multiple things. It’s hard for me to explain, if you dont understand that ask and I’ll make up a situation.

It also occured to me that you can use other characters, I found some code in some gamemodes and they have for _, t in pairs…etc.

Pairs is a function that runs next internally.

It returns three values - the key, the value, and the iterator function. You can read more on this on the manual available on www.lua.org

ipairs loops through all sequential numeric keys in a table starting at 1.

So if I have this table:



food = {
  "apple",
  "banana",
  "cantalope",
  "fruit that starts with a d",
  Price = 12.54,
  nil,
  "eggplant"
}


And used this loop:



for k, v in ipairs( food ) do
  print( k, v )
end


I get this:



1	apple
2	banana
3	cantalope
4	fruit that starts with a d


Because it sees the first four keys, and key five is nil so it stops. It never goes through non-numeric keys, such as Price in the above example.

Now, if I just use pairs:



for k, v in pairs( food ) do
  print( k, v )
end


I get:



1	apple
2	banana
3	cantalope
4	fruit that starts with a d
6	eggplant
Price	12.54


Because it finds every valid key in the table. Take note that pairs doesn’t care about the order of the table and will not always return items in the same order.

The usage of _ in the key/value setup is a habit some people use to mark that they don’t care about that value. It’s legal and has no real effect on the code.

Feel free to ask me to clarify parts of this if any of it is unclear.

If possible, you should use ipairs. It runs faster than pairs.