Log in

View Full Version : k, w and pairs??



Meliora
September 27th, 2007, 10:50
Please, can anyone explain the following line to me:


for k,w in ipairs(getWindows()) do

and a short description on what and how to use;


Child

Appreciate any help

Respect
Meliora

Dachannien
September 27th, 2007, 16:41
This Lua tutorial (https://lua-users.org/wiki/ForTutorial) probably does a better job of explaining it than I will, but I'll give it a shot anyway:

One of the ways you can use "for" in Lua is by having it iterate over a series of key and value pairs. So, if you have a list of database fields, for example, you can go through the list, putting the name of the field in one variable and the value associated with that field in the other. That's what the "for k,w" means - we're going to put the key in "k" and the value in "w".

When you use "for" in this manner, you need some way to indicate how you're going to iterate through the list. "ipairs()" is a builtin function that helps out with this. It returns two values, namely a key from the object passed to it, where the key is the next integer that hasn't been returned yet, and the value associated with that key.

So, the line "for k,w in ipairs(getWindows()) do" that you have listed above, when it's being called in the context of a windowlist object, iterates over the list of all the windows in the windowlist, in some order. (It's not clear to me what that order is, because I haven't fiddled with it.)

As for "Child", I am assuming you mean functions like "getChild()", which are called from a databasenode and return the databasenode that is a child of the current node and whose name matches the name you pass to the function. It's just a way of navigating the database tree (look inside db.xml to see more about how that works).

Meliora
September 27th, 2007, 22:54
Fantastic...thank you a lot. Exactly what I needed, to wrap my brain around this.