<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/css" href="http://wiki.lostsouls.org/w/skins/common/feed.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
	<channel>
		<title>Man foreach - Revision history</title>
		<link>http://wiki.lostsouls.org/w/index.php?title=Man_foreach&amp;action=history</link>
		<description>Revision history for this page on the wiki</description>
		<language>en</language>
		<generator>MediaWiki 1.8.2</generator>
		<lastBuildDate>Sat, 25 Apr 2026 12:43:48 GMT</lastBuildDate>
		<item>
			<title>Laine at 17:05, 11 June 2007</title>
			<link>http://wiki.lostsouls.org/w/index.php?title=Man_foreach&amp;diff=3935&amp;oldid=prev</link>
			<description>&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Synopsis==&lt;br /&gt;
&lt;br /&gt;
*foreach (&amp;lt;var&amp;gt; : &amp;lt;expr&amp;gt;) &amp;lt;statement&amp;gt;;&lt;br /&gt;
*foreach (&amp;lt;var&amp;gt;, &amp;lt;var2&amp;gt;, ... ,&amp;lt;varN&amp;gt; : &amp;lt;expr&amp;gt;) &amp;lt;statement&amp;gt;;&lt;br /&gt;
&lt;br /&gt;
/* MudOS compatibility only - not for new code: */&lt;br /&gt;
        foreach (&amp;lt;var&amp;gt; in &amp;lt;expr&amp;gt;) &amp;lt;statement&amp;gt;;&lt;br /&gt;
        foreach (&amp;lt;var&amp;gt;, &amp;lt;var2&amp;gt;, ... ,&amp;lt;varN&amp;gt; in &amp;lt;expr&amp;gt;) &amp;lt;statement&amp;gt;;&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;expr&amp;gt; is evaluated and has to yield an array, a string or a mapping.&lt;br /&gt;
The values of &amp;lt;expr&amp;gt; (in case of the string, the integer values&lt;br /&gt;
of the characters) are then assigned one after another to &amp;lt;var&amp;gt;&lt;br /&gt;
and &amp;lt;statement&amp;gt; is execute for every assignment.&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;expr&amp;gt; is a mapping, the keys are assigned to &amp;lt;var&amp;gt;, and&lt;br /&gt;
the values for each key are assigned in order to &amp;lt;var2&amp;gt;..&amp;lt;varN&amp;gt;.&lt;br /&gt;
If there are more values than variable, the extraneous values&lt;br /&gt;
are ignored.&lt;br /&gt;
&lt;br /&gt;
If there are more variables than necessary, the unneeded ones&lt;br /&gt;
are not changed.&lt;br /&gt;
&lt;br /&gt;
Every &amp;lt;var&amp;gt; specification can declare a new local variable, whose&lt;br /&gt;
scope is the whole foreach() statement.&lt;br /&gt;
&lt;br /&gt;
A 'break' in the 'statement' will terminate the loop. A&lt;br /&gt;
'continue' will continue the execution from the beginning of&lt;br /&gt;
the loop.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
WHAT HAPPENS IF &amp;lt;expr&amp;gt; IS CHANGED IN THE LOOP?&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;expr&amp;gt; yields an array:&lt;br /&gt;
         * assignments to single array elements or to array ranges effect&lt;br /&gt;
           the values assigned to the variable:&lt;br /&gt;
             a = ({1, 2, 3})&lt;br /&gt;
             foreach(x : a) { a[1..2] = ({4, 5}); write(x+&amp;quot; &amp;quot;); }&lt;br /&gt;
           will write (&amp;quot;1 4 5 &amp;quot;).&lt;br /&gt;
         * operations which implicitely copy the array (this includes&lt;br /&gt;
           range assignments which change the size) don't have an effect&lt;br /&gt;
           on the loop.&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;expr&amp;gt; yields an mapping, the loop will run over the indices&lt;br /&gt;
the mapping had at the begin of the loop. Deleted indices are silently&lt;br /&gt;
skipped, new indices ignored, but changes of the data of existing&lt;br /&gt;
indices are acknowledged.&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;expr&amp;gt; yields a string, it can't be changed anyway.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Warning==&lt;br /&gt;
&lt;br /&gt;
The additional syntax forms using &amp;quot;in&amp;quot; as keyword are meant&lt;br /&gt;
to make re-engineering of MudOS objects easier. Do not use them&lt;br /&gt;
for newly written code, as they may not be available in future.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
        &lt;br /&gt;
    // Call quit() in all interactive users&lt;br /&gt;
        foreach(o : users()) o-&amp;gt;quit();&lt;br /&gt;
        foreach(object o : users()) o-&amp;gt;quit();&lt;br /&gt;
&lt;br /&gt;
    // Print the contents of a mapping &amp;lt;m&amp;gt;&lt;br /&gt;
        foreach(key, value : m) printf(&amp;quot;%O:%O\n&amp;quot;, key, value);&lt;br /&gt;
        foreach(mixed key, mixed value : m) printf(&amp;quot;%O:%O\n&amp;quot;, key, value);&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[[man for|for(LPC)]]&lt;/div&gt;</description>
			<pubDate>Mon, 11 Jun 2007 17:05:40 GMT</pubDate>			<dc:creator>Laine</dc:creator>			<comments>http://wiki.lostsouls.org/Talk:Man_foreach</comments>		</item>
	</channel>
</rss>