<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.starfall.wtf/index.php?action=history&amp;feed=atom&amp;title=Generic_for</id>
	<title>Generic for - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.starfall.wtf/index.php?action=history&amp;feed=atom&amp;title=Generic_for"/>
	<link rel="alternate" type="text/html" href="https://wiki.starfall.wtf/index.php?title=Generic_for&amp;action=history"/>
	<updated>2026-07-28T17:09:10Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.45.3</generator>
	<entry>
		<id>https://wiki.starfall.wtf/index.php?title=Generic_for&amp;diff=475&amp;oldid=prev</id>
		<title>PRG: Created page with &quot;__TOC__  ==Introduction==  Basic for loops have already been covered in Loops.  If you haven&#039;t already done so, please review Loops.  == Discussion ==  The generic &lt;b&gt;for&lt;/b&gt; loop traverses all values returned by the iterator.  The iterator is the number that keeps track of how many times a &lt;b&gt;for&lt;/b&gt; loop is supposed to run.  This is &lt;b&gt;different&lt;/b&gt; from a the numeric &lt;b&gt;for&lt;/b&gt; loop in Loops in that before we were simply displaying the iterator (or a f...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.starfall.wtf/index.php?title=Generic_for&amp;diff=475&amp;oldid=prev"/>
		<updated>2024-12-10T22:10:36Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;__TOC__  ==Introduction==  Basic for loops have already been covered in &lt;a href=&quot;/index.php/Loops&quot; title=&quot;Loops&quot;&gt;Loops&lt;/a&gt;.  If you haven&amp;#039;t already done so, please review &lt;a href=&quot;/index.php/Loops&quot; title=&quot;Loops&quot;&gt;Loops&lt;/a&gt;.  == Discussion ==  The generic &amp;lt;b&amp;gt;for&amp;lt;/b&amp;gt; loop traverses all values returned by the &lt;a href=&quot;/index.php/Iterator&quot; title=&quot;Iterator&quot;&gt;iterator&lt;/a&gt;.  The iterator is the number that keeps track of how many times a &amp;lt;b&amp;gt;for&amp;lt;/b&amp;gt; loop is supposed to run.  This is &amp;lt;b&amp;gt;different&amp;lt;/b&amp;gt; from a the numeric &amp;lt;b&amp;gt;for&amp;lt;/b&amp;gt; loop in &lt;a href=&quot;/index.php/Loops&quot; title=&quot;Loops&quot;&gt;Loops&lt;/a&gt; in that before we were simply displaying the iterator (or a f...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
Basic for loops have already been covered in [[Loops]].  If you haven&amp;#039;t already done so, please review [[Loops]].&lt;br /&gt;
&lt;br /&gt;
== Discussion ==&lt;br /&gt;
&lt;br /&gt;
The generic &amp;lt;b&amp;gt;for&amp;lt;/b&amp;gt; loop traverses all values returned by the [[iterator]].  The iterator is the number that keeps track of how many times a &amp;lt;b&amp;gt;for&amp;lt;/b&amp;gt; loop is supposed to run.  This is &amp;lt;b&amp;gt;different&amp;lt;/b&amp;gt; from a the numeric &amp;lt;b&amp;gt;for&amp;lt;/b&amp;gt; loop in [[Loops]] in that before we were simply displaying the iterator (or a function of the iterator) itself:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Numeric for:&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i=20,1,-2 do print(i) end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now what we are doing is assigning a value to the iterator.&lt;br /&gt;
&lt;br /&gt;
Let&amp;#039;s make [[Table]] with the months of the year:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
months = {&amp;quot;January&amp;quot;, &amp;quot;February&amp;quot;, &amp;quot;March&amp;quot;, &amp;quot;April&amp;quot;, &amp;quot;May&amp;quot;, &amp;quot;June&amp;quot;, &amp;quot;July&amp;quot;,&lt;br /&gt;
 &amp;quot;August&amp;quot;, &amp;quot;September&amp;quot;, &amp;quot;October&amp;quot;, &amp;quot;November&amp;quot;, &amp;quot;December&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We know that there are twelve months in the year, and we can also call January &amp;quot;month #1&amp;quot;, February &amp;quot;month #2&amp;quot;, and so on.:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
revmonths = {[&amp;quot;January&amp;quot;] = 1, [&amp;quot;February&amp;quot;] = 2, [&amp;quot;March&amp;quot;] = 3, [&amp;quot;April&amp;quot;] = 4, [&amp;quot;May&amp;quot;] = 5, [&amp;quot;June&amp;quot;] = 6,&lt;br /&gt;
 [&amp;quot;July&amp;quot;] = 7, [&amp;quot;August&amp;quot;] = 8, [&amp;quot;September&amp;quot;] = 9, [&amp;quot;October&amp;quot;] = 10, [&amp;quot;November&amp;quot;] = 11, [&amp;quot;December&amp;quot;] = 12}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice how we assigned a unique month number to each month of the year.  Now we can find our iterators again:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
print(revmonths[&amp;quot;January&amp;quot;]) -- prints 1, which is the first month of the year&lt;br /&gt;
print(revmonths[&amp;quot;December&amp;quot;]) -- prints 12, which is the twelfth month of the year&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Script ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
months = {&amp;quot;January&amp;quot;, &amp;quot;February&amp;quot;, &amp;quot;March&amp;quot;, &amp;quot;April&amp;quot;, &amp;quot;May&amp;quot;, &amp;quot;June&amp;quot;, &amp;quot;July&amp;quot;, &amp;quot;August&amp;quot;, &amp;quot;September&amp;quot;, &amp;quot;October&amp;quot;, &lt;br /&gt;
&amp;quot;November&amp;quot;, &amp;quot;December&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
revmonths = {[&amp;quot;January&amp;quot;] = 1, [&amp;quot;February&amp;quot;] = 2, [&amp;quot;March&amp;quot;] = 3, [&amp;quot;April&amp;quot;] = 4, [&amp;quot;May&amp;quot;] = 5, [&amp;quot;June&amp;quot;] = 6, &lt;br /&gt;
[&amp;quot;July&amp;quot;] = 7, [&amp;quot;August&amp;quot;] = 8, [&amp;quot;September&amp;quot;] = 9, [&amp;quot;October&amp;quot;] = 10, [&amp;quot;November&amp;quot;] = 11, [&amp;quot;December&amp;quot;] = 12}&lt;br /&gt;
&lt;br /&gt;
print(revmonths[&amp;quot;January&amp;quot;])&lt;br /&gt;
print(revmonths[&amp;quot;February&amp;quot;])&lt;br /&gt;
print(revmonths[&amp;quot;March&amp;quot;])&lt;br /&gt;
print(revmonths[&amp;quot;April&amp;quot;])&lt;br /&gt;
print(revmonths[&amp;quot;May&amp;quot;])&lt;br /&gt;
print(revmonths[&amp;quot;June&amp;quot;])&lt;br /&gt;
print(revmonths[&amp;quot;July&amp;quot;])&lt;br /&gt;
print(revmonths[&amp;quot;August&amp;quot;])&lt;br /&gt;
print(revmonths[&amp;quot;September&amp;quot;])&lt;br /&gt;
print(revmonths[&amp;quot;October&amp;quot;])&lt;br /&gt;
print(revmonths[&amp;quot;November&amp;quot;])&lt;br /&gt;
print(revmonths[&amp;quot;December&amp;quot;])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Instead of having to slough through typing a print() command for every month, you can now do a for loop:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
months = {&amp;quot;January&amp;quot;, &amp;quot;February&amp;quot;, &amp;quot;March&amp;quot;, &amp;quot;April&amp;quot;, &amp;quot;May&amp;quot;, &amp;quot;June&amp;quot;, &amp;quot;July&amp;quot;, &amp;quot;August&amp;quot;, &amp;quot;September&amp;quot;, &amp;quot;October&amp;quot;,&lt;br /&gt;
 &amp;quot;November&amp;quot;, &amp;quot;December&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
revmonths = {&amp;quot;January&amp;quot;, &amp;quot;February&amp;quot;, &amp;quot;March&amp;quot;, &amp;quot;April&amp;quot;, &amp;quot;May&amp;quot;, &amp;quot;June&amp;quot;, &amp;quot;July&amp;quot;, &amp;quot;August&amp;quot;, &amp;quot;September&amp;quot;,&lt;br /&gt;
 &amp;quot;October&amp;quot;, &amp;quot;November&amp;quot;, &amp;quot;December&amp;quot;}&lt;br /&gt;
    &lt;br /&gt;
for i,v in ipairs(months) do&lt;br /&gt;
      revmonths[v] = i&lt;br /&gt;
print(revmonths[v])&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:Scripting Tutorials]]&lt;/div&gt;</summary>
		<author><name>PRG</name></author>
	</entry>
</feed>