<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Difference between FOR and FOR EACH loops in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Difference-between-FOR-and-FOR-EACH-loops/m-p/485870#M72976</link>
    <description>&lt;P&gt;It is not a bug -- in this instance it is actually JMP being consistent.&amp;nbsp; When indexing a list the result is returned unevaluated.&amp;nbsp; When storing the result of an indexed item into a variable it then evaluates the result.&amp;nbsp; Remember that &lt;CODE class=" language-jsl"&gt;For Each( {value, index}, list, expression... )&lt;/CODE&gt; is equivalent to &lt;CODE class=" language-jsl"&gt;For( index = 1, index &amp;lt;= N Items( list ), index++, value = list[index]; expression... )&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;A = {1 + 2, 2 + 3, 3 + 4};
b = {};
res = A[1];
Insert Into( b, A[1] );
Insert Into( b, res );
b
/*:

{1 + 2, 3}&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 10 May 2022 22:29:02 GMT</pubDate>
    <dc:creator>ErraticAttack</dc:creator>
    <dc:date>2022-05-10T22:29:02Z</dc:date>
    <item>
      <title>Difference between FOR and FOR EACH loops</title>
      <link>https://community.jmp.com/t5/Discussions/Difference-between-FOR-and-FOR-EACH-loops/m-p/485814#M72964</link>
      <description>&lt;P&gt;Can somebody explain why this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dt = Open( "$SAMPLE_DATA/Fitness.jmp" );
col = { :Runtime, :RunPulse, :RstPulse };

y expr  = Expr( Y() );
For( c = 1, c &amp;lt;= N Items( col ), c++,
	Insert Into( y expr, col[c] );
);

Name Expr(y expr);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;produces expected output&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Y( :Runtime, :RunPulse, :RstPulse )&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;but this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dt = Open( "$SAMPLE_DATA/Fitness.jmp" );
col = { :Runtime, :RunPulse, :RstPulse };

y expr  = Expr( Y() );
For each({coll, index},col,
	Insert Into( y expr, coll );
);

Name Expr(y expr);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;produces this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Y( ., ., . )&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I would expect it to give the same result? How should I change FOR EACH loop to give the same result?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Jun 2023 16:58:50 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Difference-between-FOR-and-FOR-EACH-loops/m-p/485814#M72964</guid>
      <dc:creator>miguello</dc:creator>
      <dc:date>2023-06-09T16:58:50Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between FOR and FOR EACH loops</title>
      <link>https://community.jmp.com/t5/Discussions/Difference-between-FOR-and-FOR-EACH-loops/m-p/485834#M72966</link>
      <description>&lt;P&gt;My guess is that the columns get evaluated into current rows values in For Each (you can test this by adding Row() = 1; for example). You might be able to avoid this by using Name Expr()&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1);

dt = Open("$SAMPLE_DATA/Fitness.jmp");
col = {:Runtime, :RunPulse, :RstPulse};

y expr = Expr(Y());
For(c = 1, c &amp;lt;= N Items(col), c++,
	Insert Into(y expr, col[c]);
);
Show(Name Expr(y expr));

Row() = 1;
y expr = Expr(Y());
For Each({coll, index}, col, 
	Insert Into(y expr, coll);
);
Show(Name Expr(y expr));

y expr = Expr(Y());
For Each({coll, index}, col, 
	Insert Into(y expr, Name Expr(coll));
);
Show(Name Expr(y expr));&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 10 May 2022 17:48:40 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Difference-between-FOR-and-FOR-EACH-loops/m-p/485834#M72966</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2022-05-10T17:48:40Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between FOR and FOR EACH loops</title>
      <link>https://community.jmp.com/t5/Discussions/Difference-between-FOR-and-FOR-EACH-loops/m-p/485868#M72975</link>
      <description>&lt;P&gt;Yes, this is it.&lt;/P&gt;&lt;P&gt;It is a strange behavior, since 1. we have For Each Row statement for by row evaluation, and 2. For Each should treat a list (and I'm trying to work with a list of references to columns) same as For loop.&lt;/P&gt;&lt;P&gt;Is this a bug or is it intended behavior?&lt;/P&gt;</description>
      <pubDate>Tue, 10 May 2022 22:00:44 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Difference-between-FOR-and-FOR-EACH-loops/m-p/485868#M72975</guid>
      <dc:creator>miguello</dc:creator>
      <dc:date>2022-05-10T22:00:44Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between FOR and FOR EACH loops</title>
      <link>https://community.jmp.com/t5/Discussions/Difference-between-FOR-and-FOR-EACH-loops/m-p/485870#M72976</link>
      <description>&lt;P&gt;It is not a bug -- in this instance it is actually JMP being consistent.&amp;nbsp; When indexing a list the result is returned unevaluated.&amp;nbsp; When storing the result of an indexed item into a variable it then evaluates the result.&amp;nbsp; Remember that &lt;CODE class=" language-jsl"&gt;For Each( {value, index}, list, expression... )&lt;/CODE&gt; is equivalent to &lt;CODE class=" language-jsl"&gt;For( index = 1, index &amp;lt;= N Items( list ), index++, value = list[index]; expression... )&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;A = {1 + 2, 2 + 3, 3 + 4};
b = {};
res = A[1];
Insert Into( b, A[1] );
Insert Into( b, res );
b
/*:

{1 + 2, 3}&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 10 May 2022 22:29:02 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Difference-between-FOR-and-FOR-EACH-loops/m-p/485870#M72976</guid>
      <dc:creator>ErraticAttack</dc:creator>
      <dc:date>2022-05-10T22:29:02Z</dc:date>
    </item>
  </channel>
</rss>

