## Loops Question

Occasional Contributor

Joined:

Feb 9, 2017

Hello,

I want to fill rows with 1,2,3,4, ... I know I can use row functions, but I am practicing some simple coding.

I am writing the code below in the column formula, and it fills the rows of my Test column 1,2,3,... but last row is empty. Can anyone please point out the issue? I will eventually write script in script window, but right now just practicing in column formula...

a = N Row();
For( i = 1, i <= a, i++,
Row() = i;
:Test = i;
);

Thanks!

3 REPLIES

Super User

Joined:

Jun 22, 2012

Here is a simple example of what you want to do:

Names Default To Here( 1 );
dt = Open( "\$SAMPLE_DATA\big class.jmp" );
dt << New Column( "Test" );

a = N Row( dt );
For( i = 1, i <= a, i++,
dt:Test[i] = Floor( Mod( i, 4 ) );
If( Floor( Mod( i, 4 ) ) == 0,
dt:Test[i] = 4
);
);

Here is the same thing as a formula

If( Floor( Mod( Row(), 4 ) ) == 0, 4, Floor( Mod( Row(), 4 ) ))

Formulas automatically cycle through all rows

Jim

Occasional Contributor

Joined:

Feb 9, 2017

Thanks for the help Jim!

Super User

Joined:

Mar 20, 2012

I echo what Jim said. Column Formulas are essentially an implicit loop over all rows in the table. So, in your case, to create a column that numbers every row, the column formula for the TEST column is simply:

Row()

This will number each row of the TEST column from 1 through the end of the table.

If you were to use direct scripting from a script window:

For( i = 1, i <= N Row(), i++, :Test[i] = i );

PDB