User:Merlin11188/Draft: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>Merlin11188
Started editing generic for loop page
>Merlin11188
Added a picture; added locations to the descriptions; reordered slightly
 
(22 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{CatUp|Loops}}
{{User:Merlin11188/Templates/NoEdit}}
==Introduction==


Basic for loops have already been covered in [[Loops]].  If you haven't already done so, please review [[Loops]]. Now, the generic for loop is a loop that traverses through the values returned by an [[iterator]] function. In layman's terms, an iterator function is a function that returns a function that returns some values. WHAAT? Let me show you an example:
{{Example|<pre>
function callFunc(number) -- number is the number we start at
-- This function is going to return a function
return function()
if number < 4 then -- If the number is less than 4
number = number + 1 -- Add one to it
return string.char(number + 95)
-- And return it's ascii representation plus 95. Note that there's only one value being returned.
end
end
end


for x in callFunc(1) do -- Since there's only one value being returned, you only need one variable!
{{Stub}}
print(x)
{|
end
|[[File:Home_Subpage.png|frame|A picture of the 'Home' tab on the submenu in 'My ROBLOX'.]] <br/>
for y in callFunc(3) do -- Same here.
|}
print(y)
__TOC__
end


Output:
a
b
c
c -- string.char(3 + 1 + 95) == string.char(99) == c
</pre>}}
==Discussion==


The iterator is the number that keeps track of how many times a <b>for</b> loop is supposed to run.  This is '''different''' from a the numeric '''for''' loop in [[Loops]] in that the numeric for loop there is simply an iterator:
===Character and Notifications===
{|
|On the far left of the page there is a picture of [[My_Character|your character]]. Underneath, there is a link to your system notifications.
|[[File:Avatar_Notifications.png|frame|A picture of [[My_Character|your character]] with system notifications beneath it.]]
|}


<b>Numeric for:</b>
===Best Friends===
<pre>
{|
for i=20, 1, -2 do print(i) end
|Underneath of your avatar and notification box is your [[Friends#Best_Friends|best friends]] list. Here you can see what your best friends are doing (from their [[My_Home#Status_Update|shout box]]) and whether or not they're online.
</pre>
|[[File:Best_Friends.png|frame|This a list of your [[Friends#Best_Friends|best friends]] and their most recent shouts!]]
|}


However, in the generic for loop we get the values returned by the iterator function. In the above function, callFunc, we saw that <code>string.char(number + 95)</code>, not number itself, was returned. Here's an example using multiple return values:
===Status Update===
{{Example|<pre>
{|
function times2(array)
|You can use this so that people who visit your profile can see what you're up to.
local i = 1 -- This is the iterator value
|[[File:Status Update.png|frame|This is the status update bar. People who have you as their [[Friends#Best_Friends|best friend]] will see it on their [[My_Home|home]]!]]
return function()
|}
if array[i] ~= nil then -- Make sure it's there!
i = i + 1 -- Add one to i, so this doesn't result in an infinite loop
return i - 1, array[i - 1]*2 -- return the index (i -1) and the value*2 (array[i - 1]*2)
else
end
end
end


y = {3, 4, 5}
===Feed===
for index, valTimes2 in times2(y) do -- Two, count 'em, two variables. That's because 2 values are returned!
{|
print(index, valTimes2) -- Print the index and the value times 2 to output.
|Your feed is in the center of the page, just beneath your status update box. Your feed is a way to keep you updated with all of your groups. Every time someone uses the shout box in one of your [[groups]], you're updated here!
end
|[[File:Feed bar.png|frame|This is for your feed—all of your [[groups]]' shouts go here.]]
|}


Output:
===Recently Played Games===
1 6
{|
2 8
|On the far right is the 'Recently Played Games' box. Your most recently played games can be seen here. If you want a larger list, you can click the '''See More''' button.
3 10
|[[File:Recently_Played_Games.png|frame|This is a list of your most recently played [[game]]s.]]
</pre>}}
|}


That pretty much covers generic for loops! Read on for some final examples!
===Facebook Connect===
{|
|On the far right, underneath of the 'Recently Played Games' box is the Facebook connect box. If you have a Facebook account, you can link it to your ROBLOX account! See [[connecting your account to Facebook]] for more info.
|[[File:FacebookConnect_Unconnected.png|frame|Facebook connect. You can use this to link your Facebook account to your [[Roblox|ROBLOX]] account! Your personal info will '''not''' be shared with other users!]]
|}


==Examples==
<!--
 
{| class="wikitable" style="border-spacing: 0px; padding: 0px;"
{{Example|<pre>
|-
months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
|[[File:Avatar + Notifications]]
 
|[[File:Status_Update.png]]
revmonths = {}
|[[File:Recently_Played_Games.png|287px]]
   
|-
for i, v in ipairs(months) do
|[[File:Best_Friends.png]]
revmonths[v] = i -- Make the month name the index and the location the value
|<div style="top:0px;">[[File:Feed_bar.png]]</div>
print(v, revmonths[v])
|[[File:FacebookConnect_Unconnected.png]]
end
|}-->
 
Output:
January 1 -- revmonths.January = 1
February 2 -- revmonths.February = 2
March 3
April 4
May 5 -- Cinco de mayo!
June 6
July 7
August 8
September 9
October 10
November 11
December 12
</pre>}}
 
Here's another example returning multiple values:
{{Example|<pre>
string = {}
string.gfind = function(stringToSearch, Pattern, Start)
local start = Start or 1 -- Default value is 1
return function()
local beginning, ending = stringToSearch:find(Pattern, start) -- Start searching at the specified location
if beginning and ending then -- Check to make sure that the match is there
start = ending + 1 -- Add one to the ending so the pattern will start to look after the last match
return beginning, ending, stringToSearch:sub(beginning, ending) -- return the 3 values
end
end
end
 
local stringToSearch = "Hello! My name is merlin1188!"
 
for start, finish, value in string.gfind(stringToSearch, "%a+") do
print("The match starts at " .. start ..", finishes at " .. finish .. ", and is " .. value)
end
 
Output:
The match starts at 1, finishes at 5, and is Hello
The match starts at 8, finishes at 9, and is My
The match starts at 11, finishes at 14, and is name
The match starts at 16, finishes at 17, and is is
The match starts at 19, finishes at 24, and is merlin
</pre>}}
 
==pairs and ipairs==
 
pairs and ipairs are iterator functions that return table indices and their corresponding values. pairs iterates through the entire table, even if the index is non-numerical (such as "Hi"). ipairs iterates through the table at numerical index, starting at 1 until it reaches nil. Also, please note that pairs does not iterate through the table in any particular order. Here's an example of the difference:
{{Example|<pre>
sampleTable = {[1] = "A", [2] = 2, [3] = "B", [5] = 5, Hi = "C"}
for i, v in ipairs(sampleTable) do
print(i, v)
end
print("That was with ipairs. This is with pairs.")
for i, v in pairs(sampleTable) do
print(i, v)
end
 
Output:
1 A -- 1
2 2 -- 2
3 B -- 3
That was with ipairs. This is with pairs.
2 2 -- 2
3 B -- 3
1 A -- 1
5 5 -- 5
Hi C -- Hi
</pre>}}
 
[[Category:Scripting Tutorials]]

Latest revision as of 03:31, 12 March 2012

Do not edit!
The creator of this subpage does not want it to be edited without permission. Please discuss any changes that you think are relevant on the talk page.


Stub icon Stub
This article is a stub. If you think you know more about this than we do, please help us by contacting a writer.
Thank you!


A picture of the 'Home' tab on the submenu in 'My ROBLOX'.


Character and Notifications

On the far left of the page there is a picture of your character. Underneath, there is a link to your system notifications.
A picture of your character with system notifications beneath it.

Best Friends

Underneath of your avatar and notification box is your best friends list. Here you can see what your best friends are doing (from their shout box) and whether or not they're online.
This a list of your best friends and their most recent shouts!

Status Update

You can use this so that people who visit your profile can see what you're up to.
This is the status update bar. People who have you as their best friend will see it on their home!

Feed

Your feed is in the center of the page, just beneath your status update box. Your feed is a way to keep you updated with all of your groups. Every time someone uses the shout box in one of your groups, you're updated here!
This is for your feed—all of your groups' shouts go here.

Recently Played Games

On the far right is the 'Recently Played Games' box. Your most recently played games can be seen here. If you want a larger list, you can click the See More button.
This is a list of your most recently played games.

Facebook Connect

On the far right, underneath of the 'Recently Played Games' box is the Facebook connect box. If you have a Facebook account, you can link it to your ROBLOX account! See connecting your account to Facebook for more info.
Facebook connect. You can use this to link your Facebook account to your ROBLOX account! Your personal info will not be shared with other users!