Confused in Lua

So, I’m basically confused on the whole for k, v in pairs loop.

Now, I understand it… I’ll go ahead and make a little code real quick:



Table = {}
Table[1] = 1
Table[2] = 2
Table[3] = 3

for k, v in pairs( Table ) do

	print( k, v )

end


As you can see, I understand how to create it… Also everything about it. It stands for key and value, it’s getting data from the table and doing something for how many ever table keys you have… However, I just don’t understand how this is used in GLua? I mean, what could it possibly be used for? Please help, I just don’t understand.

Thanks, please excuse my ignorance… I’m new :slight_smile:

P.S. I learn best by examples, so if you could help me, please give me a simple example of this being used in GLua, thanks :slight_smile:

Well, it could be used for a lot of things really… like if you wanted to find a player by their name-



local function GetPlayerByName( name )

    for k,v in pairs( player.GetAll() ) do 

        if v:Nick() == name then return v end

    end

end


[editline]23rd February 2016[/editline]

Mainly I use it for looping through players and entities to do checks like that one above

[editline]23rd February 2016[/editline]

I suppose with your example you could be using it to check the keys of your table to see if they were equivalent to something

I see, so basically, the player.GetAll() is getting a table of all the players. Then it asks if there’s a value in the table with the Nick() of “name” then it returns the name?

for loops – or loops in general, are extremely useful in all programming contexts. It’s most generally used for retrieving information from a list of things, say, you have a table of people’s names and phone number and you want to find a person who has a certain number. Well, you could just do:


for k,v in pairs( Names_N_Numbers ) do
   if ( v == "911" ) then
      return k -- Returns the name of the person
   end
end

[editline]22nd February 2016[/editline]

Gah, ninja’d

By the way, this is unrelated but you could create the table in your example by simply doing this:



Table = { 1, 2, 3 }


Rather than the thing you were doing… just in case you didn’t know that :stuck_out_tongue:

Well it’s the Nick() of whatever argument you send in, name is just a variable. If it finds the player with the name you are searching for, it will return the player entity.

Yeah, I knew. I just like creating tables like that. Idk maybe I’m retarded lol

[editline]22nd February 2016[/editline]

Ohhhh yeah, so basically “name” is just the variable assigned to whatever I want. Like a door or some shit?

Well yeah, it could be anything, but it’d only actually work with strings that are player names, since ply:Nick returns a string

[editline]23rd February 2016[/editline]

At least in the example there

Ahhh, yeah… I’m going to have to be doing this a lot. See, if I would’ve tried to do that in GLua, I’d probably be getting errors… Wondering why the hell I’m getting them. I guess it’s like riding a bike. It gets easier as you continue to do it.

(Off topic)
If your table does not have custom keys (i.e. keys are 1, 2, 3, etc.), it’s better to use an ipairs loop