Creating Columns in a Loop<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />Hi,
So in my script, I have a loop that creates columns, it works great, but I want to improve it a bit. I have data that comes in the form (number)|(character)|(number)|(character) etc. So I have alternating data types and I would like to print them in their normal form. Right now, the columns are in numeric form, so I can't see any of the character data. But if I change it to character format, I lose the numeric data structure. So I have this loop:
For(j = 1, j <= nitems(pinList), j++,
dt2 << New Column(pinList, Numeric, Continuous, Format("Best",12));
dt2 << New Column(pinList[j++], Character, Nominal);
);
Where I define all the column names in my pinList array. That works, except for the fact that it duplicates the entry at pinList(which in this case, is actually not a bad thing). the real problem comes in when I try to run another loop after it that parses that data which is delimited by a '|' and put it in the new columns.
For (i = 1, i <= nrows(dt2), i++,
one_test_result_list = words(column(dt2,"Test_Result"), "|");
For (k = 1, k <= nitems(one_test_result_list), k++,
Column(dt2, pinList) = one_test_result_list;
);
);
The last line runs once, and then breaks because the next entry in pinListhas a "2" after it because when the columns were created, it duplicated every name. I'm not sure how to get this to work. I can define all the names I want in the pinList array but I want alternating column types of numeric and character.
Any help is greatly appreciated! Thanks!!
Re: Creating Columns in a LoopI had tried a variation of that, but I put the j++ before the second new column line. I also used the if statement to step through my parsed data list. So I combined them like this:
For( j = 1, j <= N Items( pinList ), j++,
dt2 << New Column( pinList[j], Numeric, Continuous, Format( "Best", 12 ) );
dt2 << New Column( pinList[j + 1], Character, Nominal ); // Create column for j+1.
j++; // Then iterate by j=j+1.
);
For( i = 1, i <= N Rows( dt2 ), i++,
one_test_result_list = Words( Column( dt2, "Test_Result" ) [i], "|" );
For( j = 1, j <= N Items( one_test_result ), j++,
If( Modulo( j, 2 ) == 1,
Column( dt2, pinList[k] ) [i] = Num(one_test_result_list[k]); // Execute for odd j
Column( dt2, pinList[k] ) [i] = one_test_result_list[k]; // Execute for even j
);
);
);
This works perfectly. So it makes sure that the numeric results are printed as numbers and the characters are in their normal format. Thank you so much for your help!!
Re: Creating Columns in a LoopHi sophiaw,
If I understand your script correctly, I think the problem arises by your use of j++ within the body of your loop, because j++ will only iterate j after the loop has completed an iteration. I can think of a couple of ways around this. One method is to replace j++ with j+1, and then add a separate line for j++. The other method is to add an If() statement to create separate commands for even and odd j.
Please see Examples 1 and 2 for an example of how i++ operates within the body of a loop.
I hope this helps.
Names Default To Here( 1 );
dt2 = New Table( "dt2", Add Rows( 20 ) );
pinList = {"A", "B", "C", "D", "E", "F"};
For( j = 1, j <= N Items( pinList ), j++,
dt2 << New Column( pinList[j], Numeric, Continuous, Format( "Best", 12 ) );
dt2 << New Column( pinList[j + 1], Character, Nominal ); // Create column for j+1.
j++; // Then iterate by j=j+1.
);
For( j = 1, j <= N Items( pinList ), j++,
If( Modulo( j, 2 ) == 1,
dt2 << New Column( pinList[j], Numeric, Continuous, Format( "Best", 12 ) ), // Execute for odd j.
dt2 << New Column( pinList[j], Character, Nominal ) // Execute for even j.
)
);
// Example 1.
For( i = 1, i <= 10, i++,
Print( i );
Print( i++ );
);
// Example 2.
For( i = 1, i <= 10, i++,
Print( i );
Print( i + 1 );
i++;
);