|
| 05 Jul 2013 09:49 AM |
The lualearners tutorial doesn't go in depth. plz teach me |
|
|
| Report Abuse |
|
|
| |
|
blocco
|
  |
| Joined: 14 Aug 2008 |
| Total Posts: 29474 |
|
|
| 05 Jul 2013 09:51 AM |
| Don't think of it like that. Think of `pairs` as an iterator which gets called upon to return the function `next`, which effectively traverses, or iterates through the table that you want to traverse. |
|
|
| Report Abuse |
|
|
Thaeh
|
  |
| Joined: 05 Feb 2011 |
| Total Posts: 7685 |
|
|
| 05 Jul 2013 09:52 AM |
It's an iteration that goes by indexes and goes through every value in an assortment. It's much like for i=1,2,#children.
The useful part is ipairs, not pairs. ipairs sorts them by their indexes. |
|
|
| Report Abuse |
|
|
grimm343
|
  |
| Joined: 18 Sep 2008 |
| Total Posts: 2796 |
|
|
| 05 Jul 2013 09:52 AM |
Table = {"abc","zxy","mnas"} for _,v in pairs(Table) do print(_..", "..v) end
Output: 1, abc 2, zxy 3, mnas
for index,item in pairs(TableName) do --stuff end
The first variable is the index, the second is the item in the table. Is that what you were having trouble with? |
|
|
| Report Abuse |
|
|
grimm343
|
  |
| Joined: 18 Sep 2008 |
| Total Posts: 2796 |
|
|
| 05 Jul 2013 09:58 AM |
Thaeh, ipairs is useful unless you don't have non-numerical indices. Table = {["speed"] = 40,["health"] = 800} for _,v in pairs(Table) do print(_..", "..v) end
Output: speed, 40 health, 800
Meanwhile, if you try to use ipairs.. for _,v in ipairs(Table) do print(_..", "..v) end
Nothing appears in the output, now, because the indices are no longer numeric. |
|
|
| Report Abuse |
|
|
grimm343
|
  |
| Joined: 18 Sep 2008 |
| Total Posts: 2796 |
|
|
| 05 Jul 2013 09:58 AM |
*unless you have non-numerical indices Sorry about that. xP |
|
|
| Report Abuse |
|
|
|
| 05 Jul 2013 10:01 AM |
| Meanwhile, if you try to use pairs..* |
|
|
| Report Abuse |
|
|
grimm343
|
  |
| Joined: 18 Sep 2008 |
| Total Posts: 2796 |
|
|
| 05 Jul 2013 10:06 AM |
"Meanwhile, if you try to use ipairs.. for _,v in ipairs(Table) do print(_..", "..v) end
Nothing appears in the output, now, because the indices are no longer numeric."
pairs would give you all indices, while ipairs will only give you numeric indices. I did NOT mean pairs. >.> |
|
|
| Report Abuse |
|
|
Thaeh
|
  |
| Joined: 05 Feb 2011 |
| Total Posts: 7685 |
|
|
| 05 Jul 2013 10:14 AM |
| I had it the definitions crossed then. |
|
|
| Report Abuse |
|
|
|
| 05 Jul 2013 09:29 PM |
@grim, the second one Also what are iterators |
|
|
| Report Abuse |
|
|
|
| 05 Jul 2013 09:31 PM |
Use for i, v in next, table do Its less laggy. |
|
|
| Report Abuse |
|
|