cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Try the Materials Informatics Toolkit, which is designed to easily handle SMILES data. This and other helpful add-ins are available in the JMP® Marketplace
Choose Language Hide Translation Bar
StarfruitBob
Level VI

For loop not working as intended

Hello,

 

I have 2 very perplexing problems.

 

1 )  For the simple for loop below, I have 3 conditions that are met for a single output. I've verified by looking manually and graphing that all 8 combinations are indeed present in the data.  However, this comes out lopsided, where only x1, x2 and x3 will populate in all cells in the X1 column. The entry x2, for example, will have double the number of entries assigned to it. Upon further inspection via subset, A1, B1 and C1 columns are indeed met for both x2 and x4, which would be in equal shares. All columns share the same data & modeling type.

 

I've also blocked out the entry that is gathering double the entries, just to see what will happen. Then the entries are bundled into x2, for example.

 

For( i = 1, i <= N Rows ( dt ), i++,
	If( :A1[i] == "a1" & :B1[i] == "b1" & :C1[i] == "c1", :X1[i] = "x1";
	:A1[i] == "a1" & :B1[i] == "b1" & :C1[i] == "c2", :X1[i] = "x2";
	:A1[i] == "a1" & :B1[i] == "b2" & :C1[i] == "c1", :X1[i] = "x3";
	:A1[i] == "a1" & :B1[i] == "b2" & :C1[i] == "c2", :X1[i] = "x4";
	:A1[i] == "a2" & :B1[i] == "b1" & :C1[i] == "c1", :X1[i] = "x3";
	:A1[i] == "a2" & :B1[i] == "b1" & :C1[i] == "c2", :X1[i] = "x4";
	:A1[i] == "a2" & :B1[i] == "b2" & :C1[i] == "c1", :X1[i] = "x1";
	:A1[i] == "a2" & :B1[i] == "b2" & :C1[i] == "c2", :X1[i] = "x2";
	);
);

 

2) If I shorten the code by assigning values to cell with &, I get an error "attempting to assign to an object that is not an L-value". This is an attempt to shorten my code and not have multiple different loops.  Below is an example of 3 criterion, and 3 outputs.

For( i = 1, i <= N Rows ( dt ), i++,
	If( :A1[i] == "a1" & :B1[i] == "b1" & :C1[i] == "c1", :X1[i] = "x1" & :Y1[i] = "y1" & :Z1[i] = "z2";
	:A1[i] == "a1" & :B1[i] == "b1" & :C1[i] == "c2", :X1[i] = "x2" & :Y1[i] = "y3" & :Z1[i] = "z3";
	:A1[i] == "a1" & :B1[i] == "b2" & :C1[i] == "c1", :X1[i] = "x3" & :Y1[i] = "y2" & :Z1[i] = "z1";
	:A1[i] == "a1" & :B1[i] == "b2" & :C1[i] == "c2", :X1[i] = "x4" & :Y1[i] = "y4" & :Z1[i] = "z4";
	:A1[i] == "a2" & :B1[i] == "b1" & :C1[i] == "c1", :X1[i] = "x3" & :Y1[i] = "y1" & :Z1[i] = "z1";
	:A1[i] == "a2" & :B1[i] == "b1" & :C1[i] == "c2", :X1[i] = "x4" & :Y1[i] = "y2" & :Z1[i] = "z4";
	:A1[i] == "a2" & :B1[i] == "b2" & :C1[i] == "c1", :X1[i] = "x1" & :Y1[i] = "y4" & :Z1[i] = "z2";
	:A1[i] == "a2" & :B1[i] == "b2" & :C1[i] == "c2", :X1[i] = "x2" & :Y1[i] = "y3" & :Z1[i] = "z3";
	);
);

Any guidance would be appreciated. Thank you for your help in advanced!

Learning every day!
1 ACCEPTED SOLUTION

Accepted Solutions
stan_koprowski
Community Manager Community Manager

Re: For loop not working as intended

Hi @StarfruitBob,

You are close...

The syntax for an if function is  If( condition 1, result1, condition 2, result 2, else result ).

 

So reworking your statement by replacing the trailing semicolon with a comma:

For( i = 1, i <= N Rows ( dt ), i++,
	If(
	// condition 1 =  :A1[i] == "a1" & :B1[i] == "b1" & :C1[i] == "c1"
	// result 1 = :X1[i] = "x1" & :Y1[i] = "y1" & :Z1[i] = "z2"
		:A1[i] == "a1" & :B1[i] == "b1" & :C1[i] == "c1", :X1[i] = "x1" & :Y1[i] = "y1" & :Z1[i] = "z2"
	,
		:A1[i] == "a1" & :B1[i] == "b1" & :C1[i] == "c2", :X1[i] = "x2" & :Y1[i] = "y3" & :Z1[i] = "z3"
	,
		:A1[i] == "a1" & :B1[i] == "b2" & :C1[i] == "c1", :X1[i] = "x3" & :Y1[i] = "y2" & :Z1[i] = "z1"
	,
		:A1[i] == "a1" & :B1[i] == "b2" & :C1[i] == "c2", :X1[i] = "x4" & :Y1[i] = "y4" & :Z1[i] = "z4"
	,
		:A1[i] == "a2" & :B1[i] == "b1" & :C1[i] == "c1", :X1[i] = "x3" & :Y1[i] = "y1" & :Z1[i] = "z1"
	,
		:A1[i] == "a2" & :B1[i] == "b1" & :C1[i] == "c2", :X1[i] = "x4" & :Y1[i] = "y2" & :Z1[i] = "z4"
	,
		:A1[i] == "a2" & :B1[i] == "b2" & :C1[i] == "c1", :X1[i] = "x1" & :Y1[i] = "y4" & :Z1[i] = "z2"
	,
		:A1[i] == "a2" & :B1[i] == "b2" & :C1[i] == "c2", :X1[i] = "x2" & :Y1[i] = "y3" & :Z1[i] = "z3"
	);
);

HTH.

 

cheers,

Stan

View solution in original post

5 REPLIES 5
StarfruitBob
Level VI

Re: For loop not working as intended

I've also made sure that the column names are called correctly in the code and I've tried both commas and semi-colons at the end of a line. I've broken the code into smaller chunks, separating the possible values in column A1 into different for loops, but I have the same issue.

Learning every day!
stan_koprowski
Community Manager Community Manager

Re: For loop not working as intended

Hi @StarfruitBob,

You are close...

The syntax for an if function is  If( condition 1, result1, condition 2, result 2, else result ).

 

So reworking your statement by replacing the trailing semicolon with a comma:

For( i = 1, i <= N Rows ( dt ), i++,
	If(
	// condition 1 =  :A1[i] == "a1" & :B1[i] == "b1" & :C1[i] == "c1"
	// result 1 = :X1[i] = "x1" & :Y1[i] = "y1" & :Z1[i] = "z2"
		:A1[i] == "a1" & :B1[i] == "b1" & :C1[i] == "c1", :X1[i] = "x1" & :Y1[i] = "y1" & :Z1[i] = "z2"
	,
		:A1[i] == "a1" & :B1[i] == "b1" & :C1[i] == "c2", :X1[i] = "x2" & :Y1[i] = "y3" & :Z1[i] = "z3"
	,
		:A1[i] == "a1" & :B1[i] == "b2" & :C1[i] == "c1", :X1[i] = "x3" & :Y1[i] = "y2" & :Z1[i] = "z1"
	,
		:A1[i] == "a1" & :B1[i] == "b2" & :C1[i] == "c2", :X1[i] = "x4" & :Y1[i] = "y4" & :Z1[i] = "z4"
	,
		:A1[i] == "a2" & :B1[i] == "b1" & :C1[i] == "c1", :X1[i] = "x3" & :Y1[i] = "y1" & :Z1[i] = "z1"
	,
		:A1[i] == "a2" & :B1[i] == "b1" & :C1[i] == "c2", :X1[i] = "x4" & :Y1[i] = "y2" & :Z1[i] = "z4"
	,
		:A1[i] == "a2" & :B1[i] == "b2" & :C1[i] == "c1", :X1[i] = "x1" & :Y1[i] = "y4" & :Z1[i] = "z2"
	,
		:A1[i] == "a2" & :B1[i] == "b2" & :C1[i] == "c2", :X1[i] = "x2" & :Y1[i] = "y3" & :Z1[i] = "z3"
	);
);

HTH.

 

cheers,

Stan

StarfruitBob
Level VI

Re: For loop not working as intended

Hello Stan,

 

Thank you for your rapid reply!  This is something I've already experimented with and instead of x1, x2, and x3 populating, x1, x2, and x4 will populate, for example.

 

This also happens with multiple outputs, such as column X1 and Y1.

Learning every day!
Craige_Hales
Super User

Re: For loop not working as intended

Yes, make sure you use the commas like Stan suggests. If Secrets 

 

Edit: I think you want

If( :A1[i] == "a1" & :B1[i] == "b1" & :C1[i] == "c1" , :X1[i] = "x1" ,
	:A1[i] == "a1" & :B1[i] == "b1" & :C1[i] == "c2" , :X1[i] = "x2" ,
	:A1[i] == "a1" & :B1[i] == "b2" & :C1[i] == "c1" , :X1[i] = "x3" ,
	:A1[i] == "a1" & :B1[i] == "b2" & :C1[i] == "c2" , :X1[i] = "x4" ,
	:A1[i] == "a2" & :B1[i] == "b1" & :C1[i] == "c1" , :X1[i] = "x3" ,
	:A1[i] == "a2" & :B1[i] == "b1" & :C1[i] == "c2" , :X1[i] = "x4" ,
	:A1[i] == "a2" & :B1[i] == "b2" & :C1[i] == "c1" , :X1[i] = "x1" ,
	:A1[i] == "a2" & :B1[i] == "b2" & :C1[i] == "c2" , :X1[i] = "x2" ,
    throw("Nothing matched!") // this is the otherwise case 
);

If you really want multiple assignments, use ; rather than &

:X1[i] = "x1" & :Y1[i] = "y1" & :Z1[i] = "z2" // change this

:X1[i] = "x1"; :Y1[i] = "y1"; :Z1[i] = "z2";  // to this

While it might work with & it will be very fragile and very hard for the next person to puzzle out. As you say, the compiler sees

a=1 & b=2

as

a = ( ( 1 & b ) = 2 )

which produces the L-value message. It would be nice if the error message wasn't programmer speak for I can't store the value 2 from the right side of the = into "1 & b" on the left side of the = because "1 & b" is not a variable name.

 

Actually, the compiler does a reasonably good job for a=1 & b=2:

attempting to assign to an object that is not an L-value in access or evaluation of 'Assign' , 1 & b = /*###*/2 /*###*/

 

 

Craige
StarfruitBob
Level VI

Re: For loop not working as intended

This was literally my second attempt out of all of this, and I came back to it over the weekend again and it worked.  I don't have an explanation as to why it didn't work the first time I tried it like this.  Thank you for your response!

Learning every day!