|
| 08 Feb 2012 07:10 PM |
| Does for index, whatevernamehere ipairs(Table) call a table? |
|
|
| Report Abuse |
|
|
|
| 08 Feb 2012 07:22 PM |
No, it calls a chair.
But yes.
for index, value in pairs(Table) do ... end |
|
|
| Report Abuse |
|
|
|
| 08 Feb 2012 07:34 PM |
| I like to use ipairs for literal tables and pairs for UserData tables, such as Workspace:GetChildren(). But that's just me. Either one would work. |
|
|
| Report Abuse |
|
|
|
| 08 Feb 2012 07:35 PM |
| I've only coded 1 loop ever where I had to use ipairs |
|
|
| Report Abuse |
|
|
SDuke524
|
  |
| Joined: 29 Jul 2008 |
| Total Posts: 6267 |
|
|
| 08 Feb 2012 07:41 PM |
print(pairs(Table))
This will give you the next function and the table. |
|
|
| Report Abuse |
|
|
|
| 08 Feb 2012 07:42 PM |
Oh that's interesting!
local t = {5,8,10} print(pairs(t)(t,2))
> 3 10 |
|
|
| Report Abuse |
|
|
|
| 08 Feb 2012 07:44 PM |
| There's almost no good reason to use ipairs. Usually, you won't ever need to use ipairs. There are SOME cases where using ipairs would be better than using pairs, but such cases are really rare. |
|
|
| Report Abuse |
|
|
|
| 08 Feb 2012 07:55 PM |
@crazyman32
You didn't know that? Well, perhaps you'd be interested on how to make your own iterator functions as well? :P
For example, you could make a GetChildren function that can be used instead of pairs like this:
function GetChildren(object) local num, children = 0, object:GetChildren() return function() num = num + 1 if children[num] then return num, children[num] else return nil end end end
for a, b in GetChildren(Workspace) do -- Yes, really! print(a, b) end
Output: 1 Camera 2 Terrain |
|
|
| Report Abuse |
|
|
|
| 08 Feb 2012 09:23 PM |
| zOMG didn't know you could do that. kewl. |
|
|
| Report Abuse |
|
|
|
| 09 Feb 2012 08:00 PM |
@Julien - Yes, I've been using iteration functions of my own for a very very long time :P
I just didn't really think about using "pairs" standalone like that before |
|
|
| Report Abuse |
|
|