<?xml version="1.0" encoding="utf-8" ?><rss version="2.0" xml:base="https://www.webmaster-forums.net/crss/node/1035717" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title></title>
    <link>https://www.webmaster-forums.net/crss/node/1035717</link>
    <description></description>
    <language>en</language>
          <item>
    <title></title>
    <link>https://www.webmaster-forums.net/serverside-scripting/foreach-loop-0#comment-1206226</link>
    <description> &lt;p&gt;Cheers Abhishek, Your the Man!!!&lt;/p&gt;
 </description>
     <pubDate>Thu, 24 Aug 2006 09:30:39 +0000</pubDate>
 <dc:creator>StuPeas</dc:creator>
 <guid isPermaLink="false">comment 1206226 at https://www.webmaster-forums.net</guid>
  </item>
  <item>
    <title></title>
    <link>https://www.webmaster-forums.net/serverside-scripting/foreach-loop-0#comment-1206225</link>
    <description> &lt;p&gt;&lt;strong&gt;i&lt;/strong&gt; measures the number of comparisons made, which can vary depending on the algorithm that the &lt;strong&gt;sort&lt;/strong&gt; function uses.  This is a fairly deep subject in computing, so I won&#039;t go into much detail here.  It&#039;s well worth investigating, though.  &lt;img src=&quot;https://www.webmaster-forums.net/misc/smileys/smile.png&quot; title=&quot;Smiling&quot; alt=&quot;Smiling&quot; class=&quot;smiley-content&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Add something like &lt;strong&gt;print &quot;$i: $a $b\n&quot;;&lt;/strong&gt; after your $i++; to see what&#039;s going on.  It should print:&lt;br /&gt;
&lt;div class=&quot;codeblock&quot;&gt;&lt;code&gt;1: 500 1&lt;br /&gt;2: 400 5&lt;br /&gt;3: 1 5&lt;br /&gt;4: 5 500&lt;br /&gt;5: 500 400&lt;/code&gt;&lt;/div&gt;&#039;&lt;/p&gt;
&lt;p&gt;Which looks like &lt;a href=&quot;http://en.wikipedia.org/wiki/Mergesort&quot; class=&quot;bb-url&quot;&gt;mergesort&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;You may get something different, such as &lt;a href=&quot;http://en.wikipedia.org/wiki/Quicksort&quot; class=&quot;bb-url&quot;&gt;quicksort&lt;/a&gt;, depending on your version of Perl (5.6 and earlier use quicksort, 5.7 uses mergesort, 5.8 flips a coin).&lt;/p&gt;
 </description>
     <pubDate>Thu, 24 Aug 2006 09:08:41 +0000</pubDate>
 <dc:creator>Abhishek Reddy</dc:creator>
 <guid isPermaLink="false">comment 1206225 at https://www.webmaster-forums.net</guid>
  </item>
  <item>
    <title>extra</title>
    <link>https://www.webmaster-forums.net/serverside-scripting/foreach-loop-0#comment-1206224</link>
    <description> &lt;p&gt;I made a miskake and ran this code. I believe it may help to figure out the exact sequence of evevts taking place from the time the sort function starts to be executed.&lt;br /&gt;
&lt;div class=&quot;codeblock&quot;&gt;&lt;code&gt;sub sort_by_num&lt;br /&gt;{&lt;br /&gt;&amp;nbsp; $i++;&lt;br /&gt;&amp;nbsp; return $a &amp;lt;=&amp;gt; $b;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;$i=0;&lt;br /&gt;@arr = (500,1,400,5);&lt;br /&gt; &lt;br /&gt;foreach (sort sort_by_num(@arr))&lt;br /&gt;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print &amp;quot;$_\n;&amp;quot;;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;print &amp;quot;i = $i&amp;quot;;&lt;/code&gt;&lt;/div&gt;&#039;&lt;/p&gt;
&lt;p&gt;The output is&lt;br /&gt;
1&lt;br /&gt;
;5&lt;br /&gt;
;400&lt;br /&gt;
;500&lt;br /&gt;
;i=5 &lt;/p&gt;
&lt;p&gt;If possible could you explain each stage in the program for me.  I know its alot to ask, but ive been driven mad by this problem for 4 days now, and my course tutors are absolutely useless.&lt;/p&gt;
&lt;p&gt;I would be very very grateful.&lt;/p&gt;
&lt;p&gt;THANX&lt;br /&gt;
Stu&lt;/p&gt;
 </description>
     <pubDate>Thu, 24 Aug 2006 08:56:49 +0000</pubDate>
 <dc:creator>StuPeas</dc:creator>
 <guid isPermaLink="false">comment 1206224 at https://www.webmaster-forums.net</guid>
  </item>
  <item>
    <title>Thank you</title>
    <link>https://www.webmaster-forums.net/serverside-scripting/foreach-loop-0#comment-1206221</link>
    <description> &lt;p&gt;Thanks alot.&lt;/p&gt;
&lt;p&gt;I think I may need to know Exactly how the sort function works (which list values get compared to which and in what order, and how many times.&lt;/p&gt;
&lt;p&gt;If i run this code then &quot;i&quot; ends up being 5,, This I dont understand and probably never will until I know how sort works.&lt;/p&gt;
&lt;p&gt;[CODE]&lt;br /&gt;
#!c:\perl\bin\perl.exe&lt;/p&gt;
&lt;p&gt;sub sort_by_num&lt;br /&gt;
{&lt;br /&gt;
  $i++;&lt;br /&gt;
  return $a &amp;lt;=&amp;gt; $b;&lt;br /&gt;
}&lt;/p&gt;
&lt;p&gt;$i=0;&lt;br /&gt;
@arr = (500,1,400,5);&lt;/p&gt;
&lt;p&gt;foreach (sort sort_by_num(@arr))&lt;br /&gt;
   {&lt;br /&gt;
    print &quot;$_\n;&quot;&lt;/p&gt;
&lt;p&gt;   }&lt;/p&gt;
&lt;p&gt;print &quot;i = $i&quot;;&lt;/p&gt;
&lt;p&gt;[\CODE].&lt;/p&gt;
 </description>
     <pubDate>Thu, 24 Aug 2006 07:25:16 +0000</pubDate>
 <dc:creator>StuPeas</dc:creator>
 <guid isPermaLink="false">comment 1206221 at https://www.webmaster-forums.net</guid>
  </item>
  <item>
    <title></title>
    <link>https://www.webmaster-forums.net/serverside-scripting/foreach-loop-0#comment-1206220</link>
    <description> &lt;p&gt;Your conclusion is reasonable -- I can see how the syntax might throw you off.  In fact, this is a nuance of the &lt;strong&gt;sort&lt;/strong&gt; function, as it doesn&#039;t behave the way Perl&#039;s syntax naturally implies.  It is similar to C&#039;s sort(), if that is familiar to you.&lt;/p&gt;
&lt;p&gt;The optional first argument to &lt;strong&gt;sort&lt;/strong&gt; is a subroutine telling it what predicate to use when sorting the sequence.  The subroutine isn&#039;t evaluated just once in the foreach header -- it&#039;s evaluated many times by &lt;strong&gt;sort&lt;/strong&gt;, &lt;em&gt;instead of&lt;/em&gt; comparing ASCII values.  It only uses the default ASCII sort predicate when you supply no subroutine.&lt;/p&gt;
&lt;p&gt;Read it as &quot;sort @arr using my subroutine sort_by_num instead of your default&quot;.&lt;/p&gt;
&lt;p&gt;To be clearer, you could rewrite the parens like so:&lt;br /&gt;
&lt;code&gt;sort (sort_by_num @arr)&lt;/code&gt;&#039;&lt;/p&gt;
&lt;p&gt;Hope that helps.  &lt;img src=&quot;https://www.webmaster-forums.net/misc/smileys/smile.png&quot; title=&quot;Smiling&quot; alt=&quot;Smiling&quot; class=&quot;smiley-content&quot; /&gt;&lt;/p&gt;
 </description>
     <pubDate>Thu, 24 Aug 2006 06:09:48 +0000</pubDate>
 <dc:creator>Abhishek Reddy</dc:creator>
 <guid isPermaLink="false">comment 1206220 at https://www.webmaster-forums.net</guid>
  </item>
  </channel>
</rss>
