Subscribe Bookmark RSS Feed

Nested for loop problem

Jay

Occasional Contributor

Joined:

May 27, 2017

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

Joined:

Jun 22, 2012

Solution

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
5 REPLIES
txnelson

Super User

Joined:

Jun 22, 2012

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

Occasional Contributor

Joined:

May 27, 2017

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

Joined:

Jun 22, 2012

Solution

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

Occasional Contributor

Joined:

May 27, 2017

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

Super User

Joined:

Jun 22, 2012

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

     Help==>Books==>Scripting Guide

Jim