BookmarkSubscribe
Choose Language Hide Translation Bar
Highlighted
Jaz
Jaz
Community Trekker

Iterator Giving Wrong Value

Hi,

I am new to JSL and was trying to get the following piece of code to work:

 

dx = Open("Manually Logged Data.jmp");
Current Data Table (dx);
numberOfRowsManually = nrows(dx);
Column("End");
colManually = Column(4);
For (i = 1, i <= numberOfRowsManually, i++, print(colManually[i]));
print (i);

 

The "End" column has 14 rows and I'm trying to iterate the variable i between 1 and 14 but when I print the value of i to check it the number 15 is returned in the log. Shouldn't it return 14 because there are only 14 rows in my data table?

Any help would be appreciated.

0 Kudos
1 ACCEPTED SOLUTION

Accepted Solutions
ih
ih
Community Trekker

Re: Iterator Giving Wrong Value

To terminate the loop, JMP increases the iterator by 1 and then checks if it meets the condition you set to stay inside the loop.  In this case, you are printing the value of the iterator after it exited the loop, therefore you see the first value that did not cause the loop to execute again.  In this example, notice how 2 is the last value inside the loop, but 3 is printed as in your example:

 

Names default to here( 1 );

For( i=1, i <= 2, i++,
	print( "Inside loop: " || char( i ) );
);
print( "Outside loop: " || char( i ) );
3 REPLIES 3
ih
ih
Community Trekker

Re: Iterator Giving Wrong Value

To terminate the loop, JMP increases the iterator by 1 and then checks if it meets the condition you set to stay inside the loop.  In this case, you are printing the value of the iterator after it exited the loop, therefore you see the first value that did not cause the loop to execute again.  In this example, notice how 2 is the last value inside the loop, but 3 is printed as in your example:

 

Names default to here( 1 );

For( i=1, i <= 2, i++,
	print( "Inside loop: " || char( i ) );
);
print( "Outside loop: " || char( i ) );
Jaz
Jaz
Community Trekker

Re: Iterator Giving Wrong Value

I understand, thanks for the help!

0 Kudos

Re: Iterator Giving Wrong Value

I'm not sure if you want to understand how to use the For() function to iterate, using Print() to test, or if you really need to print each value in a data column to the Log. If it is the latter case, then this code shows a simpler way:

dx = Open( "Manually Logged Data.jmp" );
For Each Row(
	Print( dx:End );
);
Learn it once, use it forever!