cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
JMP is taking Discovery online, April 16 and 18. Register today and join us for interactive sessions featuring popular presentation topics, networking, and discussions with the experts.
Choose Language Hide Translation Bar
Jay
Jay
Level I

Nested for loop problem

Hi, I am testing a nested for loop.

 

col loop will create new columns

row loop will write in data for each row.

 

The inner row loop seems has problem, it created 1st col, but that is all.

 

====================

colNames=list();
colNames[1]="A";
colNames[2]="B";
colNames[3]="C";

dt = New Table("new");
Current Data Table(dt);

for (col=1,col<=3,col++,
 
    dt<<NewColumn(colNames[col]);
 
    for(row=1,row<=4,row++,    
    //write data into eachrow
    );

);

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: Nested for loop problem

You need to open the JMP log so you will see the errors.  I assumed the code you showed was psudo code, just showing the structure of how you wanted the code to work.  In JSL, you can not have a For loop without any arguments.  So I have adjusted the code I returned to you, so it will work

Names default to here(1);
colNames = List();
colNames[1] = "A";
colNames[2] = "B";
colNames[3] = "C";

dt = New Table( "new" );
Current Data Table( dt );

// Add Empty Rows to the Data Table
dt << Add Rows( 4 );

For( col = 1, col <= 3, col++, 
 
	dt << New Column( colNames[col] );
 
	For( row = 1, row <= 4, row++,    
    // Or you could conditionally add rows here
    // If( Col == 1, dt << Add Rows( 1 ) );
    column(dt,ColNames[col])[row]=1;
	);

);
Jim

View solution in original post

5 REPLIES 5
txnelson
Super User

Re: Nested for loop problem

The issue is that in the flow of the script that you are using, you need to explicitly add rows to your data table, using the << Add Rows() message.  See the code below for a couple of ways you could do it.  Also, the documentation for this is at:

     Help==>Scripting Index==>Data Table==>Data Table Rows==>Add Rows

Names default to here(1);
colNames = List();
colNames[1] = "A";
colNames[2] = "B";
colNames[3] = "C";

dt = New Table( "new" );
Current Data Table( dt );

// Add Empty Rows to the Data Table
dt << Add Rows( 4 );

For( col = 1, col <= 3, col++, 
 
	dt << New Column( colNames[col] );
 
	For( row = 1, row <= 4, row++,    
    // Or you could conditionally add rows here
    // If( Col == 1, dt << Add Rows( 1 ) );
	);

);
Jim
Jay
Jay
Level I

Re: Nested for loop problem

Hi, Jim
Thanks for reply. I tried above, code, it still add in the column "A" only, and column "B" "C" is not there.
txnelson
Super User

Re: Nested for loop problem

You need to open the JMP log so you will see the errors.  I assumed the code you showed was psudo code, just showing the structure of how you wanted the code to work.  In JSL, you can not have a For loop without any arguments.  So I have adjusted the code I returned to you, so it will work

Names default to here(1);
colNames = List();
colNames[1] = "A";
colNames[2] = "B";
colNames[3] = "C";

dt = New Table( "new" );
Current Data Table( dt );

// Add Empty Rows to the Data Table
dt << Add Rows( 4 );

For( col = 1, col <= 3, col++, 
 
	dt << New Column( colNames[col] );
 
	For( row = 1, row <= 4, row++,    
    // Or you could conditionally add rows here
    // If( Col == 1, dt << Add Rows( 1 ) );
    column(dt,ColNames[col])[row]=1;
	);

);
Jim
Jay
Jay
Level I

Re: Nested for loop problem

Great. Thanks Jim. First project. Kind of difficult to get started.
txnelson
Super User

Re: Nested for loop problem

I strongly suggest you read or at least scan the Scripting Guide

     Help==>Books==>Scripting Guide

Jim