<?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: Use for loop variable in a formula in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Use-for-loop-variable-in-a-formula/m-p/708674#M89269</link>
    <description>&lt;P&gt;Thx Jim, I think it is working but the print out will be slightly mismatch. It works well when adding ",1" behind&amp;nbsp; "_l_" in the formula. I attached my full jsl script here&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );
dt = Current Data Table();
//k start 40, For each loop will add 2
For( k = 40, k &amp;lt;= 54, k += 2,
	x1 = 1;
	x2 = 8;
	y1 = 40;
	y2 = 54;
	m = (y2 - y1) / (x2 - x1);
	c = y2 - (m * x2);
	l = (k - c) / m;
	
	//l is calculated based on k\
	//Tested variable &amp;amp; Column x &amp;amp; Strobe are the column in my data set
	//If k = 40;, then the readout will be 1st bit from the right of column x;
	//If k = 40;,then the readout will be 8th bit from the right of column x;
	
	// The variable l needs to be fully parsed in the formula, so that if the 
	// value of l changes, it does not change previous formulas
	Eval(
		Substitute(
				Expr(
					dt &amp;lt;&amp;lt; New Column( "Test" || Char( k ),
						Numeric,
						"Continuous",
						Format( "Best", 12 ),
						Formula(
							theVal = .;
							If( :Strobe == 1,
								If( :Tested Variable == 0 | :Tested Variable == 1,
									theVal = Num( Substr( :x, _l_ ,1) )
								)
							);
							theVal;
						)
					)
				),
			Expr( _l_ ), l
		)
	);
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 13 Dec 2023 08:07:02 GMT</pubDate>
    <dc:creator>Sysy13</dc:creator>
    <dc:date>2023-12-13T08:07:02Z</dc:date>
    <item>
      <title>Use for loop variable in a formula</title>
      <link>https://community.jmp.com/t5/Discussions/Use-for-loop-variable-in-a-formula/m-p/708063#M89225</link>
      <description>&lt;P&gt;Trying with this script but variable "l" is not working, only variable "k" is working. Anyway to solve this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;For( k = 40, k &amp;lt;= 54, k += 2,
	x1 = 1;
	x2 = 8;
	y1 = 40;
	y2 = 54;
	m = (y2 - y1) / (x2 - x1);
	c = y2 - (m * x1);
	l = (k - c) / m;
	dt &amp;lt;&amp;lt; New Column( "Test" || Char( k ),
		Numeric,
		"Continuous",
		Format( "Best", 12 ),
		Formula(
			If( :Strobe == 1,
				If(
					:Tested Variable == 0 | :Tested Plane == 1,
						If( Left( Right( :"x"n, Column[l] ) ), 1 ) == "1",
					1, Left( Right( :"x"n, (l) ), 1 ) == "0",
					0,
				), 

			)
		)
	);
)&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 12 Dec 2023 00:38:51 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Use-for-loop-variable-in-a-formula/m-p/708063#M89225</guid>
      <dc:creator>Sysy13</dc:creator>
      <dc:date>2023-12-12T00:38:51Z</dc:date>
    </item>
    <item>
      <title>Re: Use for loop variable in a formula</title>
      <link>https://community.jmp.com/t5/Discussions/Use-for-loop-variable-in-a-formula/m-p/708071#M89226</link>
      <description>&lt;P&gt;A couple of issues&lt;/P&gt;
&lt;P&gt;The&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Column[ l ]&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;you specified is incorrect syntax.&amp;nbsp; Column is a function (), not a matrix [] therefore it needs to be specified as&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Column( l )&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Next, the formula will have an issue with the value you are assigning.&amp;nbsp; Given your formula, there are cases where no value will be assigned.&amp;nbsp; Since a JMP formula returns the last value assigned, you need to explicitly assign a value to the formula for every case.&amp;nbsp; In my version of your formula, I am using a variable&amp;nbsp; X to assign your logic to.&amp;nbsp; I initialize the value to a missing value, and then X is assigned by your If() logic.&amp;nbsp; I then assure that the calculated value of X is the last value processed, by simply adding&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;x;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;as the last statement in the formula.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;For( k = 40, k &amp;lt;= 54, k += 2,
	x1 = 1;
	x2 = 8;
	y1 = 40;
	y2 = 54;
	m = (y2 - y1) / (x2 - x1);
	c = y2 - (m * x1);
	l = (k - c) / m;
	dt &amp;lt;&amp;lt; New Column( "Test" || Char( k ),
		Numeric,
		"Continuous",
		Format( "Best", 12 ),
		Formula(
			x = .;
			If( :Strobe == 1,
				If(
					:Tested Variable == 0 | :Tested Plane == 1,
						If( Left( Right( :"x"n, Column(l) ) ),
							1
						) == "1",
					x = 1, 
					Left( Right( :"x"n, (l) ), 1 ) == "0",
					x = 0,
				), 

			);
			x;
		)
	);
)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Finally, when entering JSL into a Discussion Question, please use the&amp;nbsp;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="txnelson_0-1702341399738.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/59538i266EC57903945415/image-size/medium?v=v2&amp;amp;px=400" role="button" title="txnelson_0-1702341399738.png" alt="txnelson_0-1702341399738.png" /&gt;&lt;/span&gt;&amp;nbsp;icon so the JSL is easier to read by the Community Members&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Dec 2023 00:37:43 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Use-for-loop-variable-in-a-formula/m-p/708071#M89226</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2023-12-12T00:37:43Z</dc:date>
    </item>
    <item>
      <title>Re: Use for loop variable in a formula</title>
      <link>https://community.jmp.com/t5/Discussions/Use-for-loop-variable-in-a-formula/m-p/708595#M89260</link>
      <description>&lt;P&gt;Ok sure sure, will use the icon for next time. Btw the script is not working. Maybe I should further elaborate what are the problems I faced&lt;BR /&gt;&lt;BR /&gt;I have a column with 8bit binary number, for example x="00101101"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then when my variable k is different, the bit read out from column x should be different&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;\\k start 40, For each loop will add 2
For( k = 40, k &amp;lt;= 54, k += 2,
	x1 = 1;
	x2 = 8;
	y1 = 40;
	y2 = 54;
	m = (y2 - y1) / (x2 - x1);
	c = y2 - (m * x2);
	l = (k - c) / m;
\\l is calculated based on k\
\\Tested variable &amp;amp; Column x &amp;amp; Strobe are the column in my data set
\\If k=40, then the readout will be 1st bit from the right of column x;
\\If k=40, then the readout will be 8th bit from the right of column x;
	dt &amp;lt;&amp;lt; New Column( "Test" || Char( k ),
		Numeric,
		"Continuous",
		Format( "Best", 12 ),
		Formula(
			If( :Strobe == 1,
				If(
					:Tested Variable == 0 | :Tested Variable == 1,
						If(  Right( :"x"n, Column(l) ) ) == "1",
					1, Right( :"x"n, (l) )== "0",
					0,
				), 

			)
		)
	);
)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The issue I faced is jmp cannot recognize what is the bit that I want to read out no matter I put Column(l), l&amp;nbsp; in the for loop&lt;/P&gt;</description>
      <pubDate>Wed, 13 Dec 2023 00:03:33 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Use-for-loop-variable-in-a-formula/m-p/708595#M89260</guid>
      <dc:creator>Sysy13</dc:creator>
      <dc:date>2023-12-13T00:03:33Z</dc:date>
    </item>
    <item>
      <title>Re: Use for loop variable in a formula</title>
      <link>https://community.jmp.com/t5/Discussions/Use-for-loop-variable-in-a-formula/m-p/708598#M89261</link>
      <description>&lt;P&gt;It would be helpful to if you could attach a sample data table.&lt;/P&gt;
&lt;P&gt;With your further explanation, try changing&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;If(  Right( :"x"n, Column(l) ) ) == "1"&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;to&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;If(  Right( :"x"n, as Column( "Column" || char(l) )== "1"&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 13 Dec 2023 00:26:22 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Use-for-loop-variable-in-a-formula/m-p/708598#M89261</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2023-12-13T00:26:22Z</dc:date>
    </item>
    <item>
      <title>Re: Use for loop variable in a formula</title>
      <link>https://community.jmp.com/t5/Discussions/Use-for-loop-variable-in-a-formula/m-p/708603#M89263</link>
      <description>&lt;P&gt;Hi Jim, here's data table, I put what I expect to see in data table. Column data describe what is the "l" value and which value of "x" it is coming from&lt;/P&gt;</description>
      <pubDate>Wed, 13 Dec 2023 02:50:02 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Use-for-loop-variable-in-a-formula/m-p/708603#M89263</guid>
      <dc:creator>Sysy13</dc:creator>
      <dc:date>2023-12-13T02:50:02Z</dc:date>
    </item>
    <item>
      <title>Re: Use for loop variable in a formula</title>
      <link>https://community.jmp.com/t5/Discussions/Use-for-loop-variable-in-a-formula/m-p/708620#M89265</link>
      <description>&lt;P&gt;The below script appears to work.&amp;nbsp; Your supplied script used \\ as comment initiators, where // is what the correct syntax is.&lt;/P&gt;
&lt;P&gt;The real working formula really boiled down to using the Substr() function.&lt;/P&gt;
&lt;P&gt;The addition of the Eval(Substitute(Expr())) section just changed the value of your l variable into the actual value of l, rather than having the value referenced through the variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );
dt = Current Data Table();
//k start 40, For each loop will add 2
For( k = 40, k &amp;lt;= 54, k += 2,
	x1 = 1;
	x2 = 8;
	y1 = 40;
	y2 = 54;
	m = (y2 - y1) / (x2 - x1);
	c = y2 - (m * x2);
	l = (k - c) / m;
	
	//l is calculated based on k\
	//Tested variable &amp;amp; Column x &amp;amp; Strobe are the column in my data set
	//If k = 40;, then the readout will be 1st bit from the right of column x;
	//If k = 40;,then the readout will be 8th bit from the right of column x;
	
	// The variable l needs to be fully parsed in the formula, so that if the 
	// value of l changes, it does not change previous formulas
	Eval(
		Substitute(
				Expr(
					dt &amp;lt;&amp;lt; New Column( "Test" || Char( k ),
						Numeric,
						"Continuous",
						Format( "Best", 12 ),
						Formula(
							theVal = .;
							If( :Strobe == 1,
								If( :Tested Variable == 0 | :Tested Variable == 1,
									theVal = Num( Substr( :x, _l_ , 1 ) )
								)
							);
							theVal;
						)
					)
				),
			Expr( _l_ ), l
		)
	);
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 13 Dec 2023 14:00:33 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Use-for-loop-variable-in-a-formula/m-p/708620#M89265</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2023-12-13T14:00:33Z</dc:date>
    </item>
    <item>
      <title>Re: Use for loop variable in a formula</title>
      <link>https://community.jmp.com/t5/Discussions/Use-for-loop-variable-in-a-formula/m-p/708674#M89269</link>
      <description>&lt;P&gt;Thx Jim, I think it is working but the print out will be slightly mismatch. It works well when adding ",1" behind&amp;nbsp; "_l_" in the formula. I attached my full jsl script here&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );
dt = Current Data Table();
//k start 40, For each loop will add 2
For( k = 40, k &amp;lt;= 54, k += 2,
	x1 = 1;
	x2 = 8;
	y1 = 40;
	y2 = 54;
	m = (y2 - y1) / (x2 - x1);
	c = y2 - (m * x2);
	l = (k - c) / m;
	
	//l is calculated based on k\
	//Tested variable &amp;amp; Column x &amp;amp; Strobe are the column in my data set
	//If k = 40;, then the readout will be 1st bit from the right of column x;
	//If k = 40;,then the readout will be 8th bit from the right of column x;
	
	// The variable l needs to be fully parsed in the formula, so that if the 
	// value of l changes, it does not change previous formulas
	Eval(
		Substitute(
				Expr(
					dt &amp;lt;&amp;lt; New Column( "Test" || Char( k ),
						Numeric,
						"Continuous",
						Format( "Best", 12 ),
						Formula(
							theVal = .;
							If( :Strobe == 1,
								If( :Tested Variable == 0 | :Tested Variable == 1,
									theVal = Num( Substr( :x, _l_ ,1) )
								)
							);
							theVal;
						)
					)
				),
			Expr( _l_ ), l
		)
	);
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 13 Dec 2023 08:07:02 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Use-for-loop-variable-in-a-formula/m-p/708674#M89269</guid>
      <dc:creator>Sysy13</dc:creator>
      <dc:date>2023-12-13T08:07:02Z</dc:date>
    </item>
    <item>
      <title>Re: Use for loop variable in a formula</title>
      <link>https://community.jmp.com/t5/Discussions/Use-for-loop-variable-in-a-formula/m-p/708796#M89289</link>
      <description>&lt;P&gt;You are correct, that is my error.&amp;nbsp; I will correct my entry.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Dec 2023 13:59:18 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Use-for-loop-variable-in-a-formula/m-p/708796#M89289</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2023-12-13T13:59:18Z</dc:date>
    </item>
  </channel>
</rss>

