<?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 Confusion with For Each resolving the value variable in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Confusion-with-For-Each-resolving-the-value-variable/m-p/761405#M93933</link>
    <description>&lt;P&gt;In solving&amp;nbsp;&lt;A href="https://community.jmp.com/t5/Discussions/Iteratively-create-columns-containing-formulae/m-p/761351#M93923" target="_self"&gt;Iteratively create columns containing formulae&lt;/A&gt;&amp;nbsp;I ran across a need to structure the solution in a way that I do not understand why.&amp;nbsp; My initial solution was&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );
dt = 
// Open Data Table: semiconductor capability.jmp
// → Data Table( "semiconductor capability" )
Open( "$SAMPLE_DATA/semiconductor capability.jmp" );

colNames = dt &amp;lt;&amp;lt; get column names( continuous, string );

For Each( {col}, colNames,
	theTarget = (Column( Eval( col ) ) &amp;lt;&amp;lt; get property( "Spec Limits" ))["Target"];
	Eval(
		Eval Expr(
			New Column( col || " Minus Target",
				Numeric,
				"Continuous",
				Format( "Best", 10 ),
				Formula( As Column( Eval( col ) ) - Expr( theTarget ) )
			)
		)
	);
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And it fails for&lt;/P&gt;
&lt;PRE&gt;&lt;BR /&gt;Column NPN1 Minus Target Formula Interrupted
Name Unresolved: col at row 1 in access or evaluation of 'col' , col/*###*/
Formula evaluation errors have been ignored

Column NPN1 Minus Target Formula Interrupted
Name Unresolved: col at row 1001 in access or evaluation of 'col' , col/*###*/
Formula evaluation errors have been ignored&lt;/PRE&gt;
&lt;P&gt;While by forcing an assignment of the variable&amp;nbsp; col&amp;nbsp; The code will work&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );
dt = 
// Open Data Table: semiconductor capability.jmp
// → Data Table( "semiconductor capability" )
Open( "$SAMPLE_DATA/semiconductor capability.jmp" );

colNames = dt &amp;lt;&amp;lt; get column names( continuous, string )&lt;BR /&gt;&lt;BR /&gt;For Each( {xcol}, colNames,
	col = xcol;
	theTarget = (Column( Eval( col ) ) &amp;lt;&amp;lt; get property( "Spec Limits" ))["Target"];
	Eval(
		Eval Expr(
			New Column( col || " Minus Target",
				Numeric,
				"Continuous",
				Format( "Best", 10 ),
				Formula( As Column( Eval( col ) ) - Expr( theTarget ) )
			)
		)
	);
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Also, once I run the code with&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;	col = xcol;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I can run the code without the xcol assignment, and the original code will work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is confusing to me.&amp;nbsp; Can anyone explain the reasoning behind this?&lt;/P&gt;</description>
    <pubDate>Fri, 24 May 2024 20:11:25 GMT</pubDate>
    <dc:creator>txnelson</dc:creator>
    <dc:date>2024-05-24T20:11:25Z</dc:date>
    <item>
      <title>Confusion with For Each resolving the value variable</title>
      <link>https://community.jmp.com/t5/Discussions/Confusion-with-For-Each-resolving-the-value-variable/m-p/761405#M93933</link>
      <description>&lt;P&gt;In solving&amp;nbsp;&lt;A href="https://community.jmp.com/t5/Discussions/Iteratively-create-columns-containing-formulae/m-p/761351#M93923" target="_self"&gt;Iteratively create columns containing formulae&lt;/A&gt;&amp;nbsp;I ran across a need to structure the solution in a way that I do not understand why.&amp;nbsp; My initial solution was&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );
dt = 
// Open Data Table: semiconductor capability.jmp
// → Data Table( "semiconductor capability" )
Open( "$SAMPLE_DATA/semiconductor capability.jmp" );

colNames = dt &amp;lt;&amp;lt; get column names( continuous, string );

For Each( {col}, colNames,
	theTarget = (Column( Eval( col ) ) &amp;lt;&amp;lt; get property( "Spec Limits" ))["Target"];
	Eval(
		Eval Expr(
			New Column( col || " Minus Target",
				Numeric,
				"Continuous",
				Format( "Best", 10 ),
				Formula( As Column( Eval( col ) ) - Expr( theTarget ) )
			)
		)
	);
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And it fails for&lt;/P&gt;
&lt;PRE&gt;&lt;BR /&gt;Column NPN1 Minus Target Formula Interrupted
Name Unresolved: col at row 1 in access or evaluation of 'col' , col/*###*/
Formula evaluation errors have been ignored

Column NPN1 Minus Target Formula Interrupted
Name Unresolved: col at row 1001 in access or evaluation of 'col' , col/*###*/
Formula evaluation errors have been ignored&lt;/PRE&gt;
&lt;P&gt;While by forcing an assignment of the variable&amp;nbsp; col&amp;nbsp; The code will work&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );
dt = 
// Open Data Table: semiconductor capability.jmp
// → Data Table( "semiconductor capability" )
Open( "$SAMPLE_DATA/semiconductor capability.jmp" );

colNames = dt &amp;lt;&amp;lt; get column names( continuous, string )&lt;BR /&gt;&lt;BR /&gt;For Each( {xcol}, colNames,
	col = xcol;
	theTarget = (Column( Eval( col ) ) &amp;lt;&amp;lt; get property( "Spec Limits" ))["Target"];
	Eval(
		Eval Expr(
			New Column( col || " Minus Target",
				Numeric,
				"Continuous",
				Format( "Best", 10 ),
				Formula( As Column( Eval( col ) ) - Expr( theTarget ) )
			)
		)
	);
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Also, once I run the code with&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;	col = xcol;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I can run the code without the xcol assignment, and the original code will work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is confusing to me.&amp;nbsp; Can anyone explain the reasoning behind this?&lt;/P&gt;</description>
      <pubDate>Fri, 24 May 2024 20:11:25 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Confusion-with-For-Each-resolving-the-value-variable/m-p/761405#M93933</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2024-05-24T20:11:25Z</dc:date>
    </item>
    <item>
      <title>Re: Confusion with For Each resolving the value variable</title>
      <link>https://community.jmp.com/t5/Discussions/Confusion-with-For-Each-resolving-the-value-variable/m-p/761763#M93948</link>
      <description>&lt;P&gt;Most likely someone from JMP has to provide the correct answer . &lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/11257"&gt;@EvanMcCorkle&lt;/a&gt; ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm not sure about the exact reason, but one reason is that the col won't get evaluated inside the formula (&lt;STRONG&gt;change Eval() to Expr()&lt;/STRONG&gt; to force column name inside As Column()) (Or I think you can run &amp;lt;&amp;lt; run formulas after formula creation and then remove formula immediately to make sure it won't get re-evaluated with incorrect column).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If col is the variable you store for each container values, it won't exist outside the loop with those values (setting xcol to col somewhat bypasses this)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1);

a = {"a", "b", "c"};
b = "z";

show(b);
For Each({b}, a,
	show(b);
);
show(b);

/*
b = "z";
b = "a";
b = "b";
b = "c";
b = "z";
*/&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I prefer using this method (Expr(NameExpr(AsColumn())) as this leaves the formula "cleaner" (:name instead of AsColumn("name").&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1);
dt = Open("$SAMPLE_DATA/semiconductor capability.jmp");

colNames = (dt &amp;lt;&amp;lt; get column names(continuous, string))[1::10];

For Each({col}, colNames,
	theTarget = (Column(Eval(col)) &amp;lt;&amp;lt; get property("Spec Limits"))["Target"];
	Eval(
		Eval Expr(
			New Column(col || " Minus Target",
				Numeric,
				"Continuous",
				Format("Best", 10),
				Formula(Expr(NameExpr(As Column(col))) - Expr(theTarget))
			)
		)
	);
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is most likely not the same issues as ... Each loops have with function local scope (this is extremely annoying ... Each loops do not behave properly with function's local scope. I have reported this to JMP support)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1);

b = 2;

my_f = function({a}, {Default Local},

	c = Transform Each({item}, a,
		a * b
	);
	return(c);
);

my_f2 = function({a}, {Default Local},
	
	b = b; // "Fix"
	c = Transform Each({item}, a,
		a * b
	);
	return(c);
);

Try(
	show(my_f({1,2,3}));
,
	show(exception_msg);
);

Try(
	show(my_f2({1,2,3}));
,
	show(exception_msg);
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 26 May 2024 15:43:50 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Confusion-with-For-Each-resolving-the-value-variable/m-p/761763#M93948</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2024-05-26T15:43:50Z</dc:date>
    </item>
  </channel>
</rss>

