generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: Table, ipairs!

Previous Thread :: Next Thread 
RobloxNewb01 is not online. RobloxNewb01
Joined: 16 Apr 2011
Total Posts: 348
08 Feb 2012 07:10 PM
Does for index, whatevernamehere ipairs(Table) call a table?
Report Abuse
crazyman32 is online. crazyman32
Joined: 13 Apr 2008
Total Posts: 18027
08 Feb 2012 07:22 PM
No, it calls a chair.

But yes.

for index, value in pairs(Table) do
...
end
Report Abuse
epicfail22 is not online. epicfail22
Joined: 25 Sep 2009
Total Posts: 3739
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
crazyman32 is online. crazyman32
Joined: 13 Apr 2008
Total Posts: 18027
08 Feb 2012 07:35 PM
I've only coded 1 loop ever where I had to use ipairs
Report Abuse
SDuke524 is not online. 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
crazyman32 is online. crazyman32
Joined: 13 Apr 2008
Total Posts: 18027
08 Feb 2012 07:42 PM
Oh that's interesting!

local t = {5,8,10} print(pairs(t)(t,2))

> 3 10
Report Abuse
JulienDethurens is not online. JulienDethurens
Joined: 11 Jun 2009
Total Posts: 11046
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
JulienDethurens is not online. JulienDethurens
Joined: 11 Jun 2009
Total Posts: 11046
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
epicfail22 is not online. epicfail22
Joined: 25 Sep 2009
Total Posts: 3739
08 Feb 2012 09:23 PM
zOMG didn't know you could do that. kewl.
Report Abuse
crazyman32 is online. crazyman32
Joined: 13 Apr 2008
Total Posts: 18027
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
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image