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 » Scripters
Home Search
 

Re: What is the "in" operator/statement(idk)

Previous Thread :: Next Thread 
thebijespun is not online. thebijespun
Joined: 24 Feb 2011
Total Posts: 400
22 Jun 2016 09:55 PM
I couldn't find anything on the Roblox Wiki or the RBX.Lua Glossary. What does "in" mean in LUA? Provide me with a link if you can please.
Example:
for i, v "in" pairs(table)
--stuff here that uses pairs() function to return table
--pairs(t)
end


Report Abuse
cntkillme1 is not online. cntkillme1
Joined: 16 Feb 2012
Total Posts: 592
22 Jun 2016 10:04 PM
It's a 'keyword' used only in the generic for loop.

So before we get into generic for loops, there is a tiny bit of background you need to know about the types of loops in Lua:

- While loop (checks condition, runs code, repeat)
- Repeat loop (runs code, checks condition, repeat)
- Numerical for loop (uses a counter)
- Generic for loop (uses function calls)

A numerical for loop is very straightforward:
for counter = initial, final [, step] do
initial is the value the counter is initially set to, and final is the value in which when the counter reaches that value, the loop terminates. The step is optional and defaults to 1, it defines how much to increment counter by.

for x = 1, 10 do
print(x)
end
will print the numbers 1 through 10

for x = 6, 2, -2 do
print(x)
end
will print 6, 4, 2

A generic for loop is less straightforward but still relatively simple to understand: the loop is terminated if the control variable (the first value returned by the iterator function (the function repeatedly called)) becomes nil.
It is also implicitly passed to the iterator function for convenience.
A simple example would be the pairs iterator (well technically pairs is _not_ the iterator, but rather returns the iterator):

for index, value in pairs(tbl) do print(index, value) end
Before the loop even starts, the "pairs(tbl)" expression is evaluated. All the pairs function does is return next (the iterator itself), the table (tbl), and nil (initial value of the control variable).

So doing that is similar to doing this:
local index, value = next(tbl)
while index ~= nil do
print(index, value)
index, value = next(tbl, index)
end

The next simply returns the next key-value pair given the previous key (or the "first" key-value pair is nil is given as the key, which is what happens initially. next(tbl) and next(tbl, nil) are more or less the exact same).

You can go beyond and make your own iterators if you want, but once you understand generic for loops, a lot of other iterators start to make more sense logically.

If you need clarification just ask.
Report Abuse
TimeTicks is not online. TimeTicks
Joined: 27 Apr 2011
Total Posts: 27115
22 Jun 2016 10:10 PM
ive always read it in my mind as

for every index and value in a table do

basically you are getting everything 'in' a table.


Report Abuse
cntkillme1 is not online. cntkillme1
Joined: 16 Feb 2012
Total Posts: 592
22 Jun 2016 10:19 PM
You've been reading it wrong. Back in older versions of Lua that was more or less true but with iterators, you don't have to be only dealing with tables or only dealing with a "key" and a "value"

for example string.gmatch:

for a,b,c,d in str:gmatch("(%a)%a(%a)%a(%a)%a(%a)%a") do
end

Would get every odd letter in str (assuming it follows that format) so if the str was the alphabet:
iter 1: a = "a", b = "c", c = "e", d = "g"
iter 2: a = "i", b = "k" c = "n" d = "p"
and so on.
Report Abuse
thebijespun is not online. thebijespun
Joined: 24 Feb 2011
Total Posts: 400
22 Jun 2016 10:30 PM
ok so i have a couple of questions.
since a iterator function gives you an iterator, then how would the pair() iterator function return the table? I can understand how it can return nil but a table???
so if the thing was table = {1,2}
for i, v in pairs do
print(i,v)
end
it would give you an output of 1,1 and 2,2 << are those the 2 iterators?


Report Abuse
thebijespun is not online. thebijespun
Joined: 24 Feb 2011
Total Posts: 400
22 Jun 2016 10:32 PM
i ment pairs(table)***


Report Abuse
cntkillme1 is not online. cntkillme1
Joined: 16 Feb 2012
Total Posts: 592
22 Jun 2016 10:34 PM
Let's say this is your code:

local t = {1, 2, 3}
for i,v in pairs(t) do
end

pairs(t) is practically just doing this:
local function pairs(tbl)
return next, tbl, nil
end

So then the loop now becomes:
for i, v in next, tbl, nil do

Which is the true nature of a generic for loop
Report Abuse
thebijespun is not online. thebijespun
Joined: 24 Feb 2011
Total Posts: 400
22 Jun 2016 10:41 PM
so "next" is the iterator, tbl is the value of the it, and nil is just nothing.
what about when the wiki said that ipairs returns 0 and pairs returns nil.
why does it return nil or 0? is nil a value as well?


Report Abuse
ranvir2050 is not online. ranvir2050
Joined: 20 Dec 2014
Total Posts: 321
22 Jun 2016 10:46 PM
U are all so wrong, u are supposed to use the returning function WHILE THRUSE DO
Report Abuse
cntkillme1 is not online. cntkillme1
Joined: 16 Feb 2012
Total Posts: 592
22 Jun 2016 10:54 PM
'what about when the wiki said that ipairs returns 0 and pairs returns nil.'
Where does it say that?

The loop terminates when the iterator function (next) returns nil
Report Abuse
thebijespun is not online. thebijespun
Joined: 24 Feb 2011
Total Posts: 400
22 Jun 2016 11:22 PM
I thought next was the iterator not the iterator function


Report Abuse
cntkillme1 is not online. cntkillme1
Joined: 16 Feb 2012
Total Posts: 592
22 Jun 2016 11:23 PM
An iterator is a function.
'pairs' and 'ipairs' and all those are called "factories"
Report Abuse
thebijespun is not online. thebijespun
Joined: 24 Feb 2011
Total Posts: 400
22 Jun 2016 11:26 PM
well since 'pairs' and 'ipairs' are in the function dump then i'm guessing they are functions. I'm guessing they are iterator functions that return iterators. i'm just trying to figure out what the iterators are.


Report Abuse
cntkillme1 is not online. cntkillme1
Joined: 16 Feb 2012
Total Posts: 592
22 Jun 2016 11:27 PM
They are functions* that return iterators (which happen to also be functions).
Report Abuse
thebijespun is not online. thebijespun
Joined: 24 Feb 2011
Total Posts: 400
22 Jun 2016 11:33 PM
so in the script:
tbl = {1,2,3,4,5,}
for i,v in pairs(tbl) do
print(i,v)
end
output:
1,1
2,2
3,3
4,4
5,5

what would be the iterator(im guessing the integer key)?


Report Abuse
cntkillme1 is not online. cntkillme1
Joined: 16 Feb 2012
Total Posts: 592
22 Jun 2016 11:49 PM
the next function
i already told you
Report Abuse
thebijespun is not online. thebijespun
Joined: 24 Feb 2011
Total Posts: 400
23 Jun 2016 12:03 AM
oh ok thx i get it now ;)


Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • 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