cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Have your say in shaping JMP's future by participating in the new JMP Wish List Prioritization Survey
Choose Language Hide Translation Bar
dadawasozo
Level IV

Subscript range issue when doing increasing for loop

I got error "Subscript Range in access or evaluation of 'collist[ /*###*/i]' , collist[/*###*/i]" when doing a simple new column () with increasing for loop. I able to solve it using decreasing for loop. However, I want to have created column ordered follow increasing for loop. I dont understand what causes the error when using increasing for loop here. Can someone provide suggestion how I can use increasing for loop here? if possible, can someone help explain why the error will happen for increasing for loop?

path = "my file path with filename.jmp";

dt= open(path);
collist = dt <<get column names(string);
colname = Column("test1") <<getname;
colnumber = (Loc(collist,colname))[1];

For(i=colnumber+1, i<=Ncols(dt),i++,  //give the error
//For(i=Ncols(dt), i>colnumber+1,i--,  fixed issue but created column order inverted

dt  <<new column("SG_"||collist[i], character);

);



1 ACCEPTED SOLUTION

Accepted Solutions
Thierry_S
Super User

Re: Subscript range issue when doing increasing for loop

Hi,

I think that your problem is that N Cols (dt) increases after each iteration of your loop. Therefore, your index value "i" exceeds the number of elements in your collist list.

 

I suggest capturing the number of columns in dt outside the loop and using it as a fixed limit. 

path = "my file path with filename.jmp";

dt= open(path);
collist = dt <<get column names(string);
colname = Column("test1") <<getname;
colnumber = (Loc(collist,colname))[1];
num_col = N Cols (dt);

For(i=colnumber+1, i<=num_col,i++,  
   dt  <<new column("SG_"||collist[i], character);

);

Of note, I am unsure why the reverse loop works. You it should run into a similar issue with your collist variable.

Let us know if that solve your issue.

Best,

TS

 

 

 

Thierry R. Sornasse

View solution in original post

3 REPLIES 3
Thierry_S
Super User

Re: Subscript range issue when doing increasing for loop

Hi,

I think that your problem is that N Cols (dt) increases after each iteration of your loop. Therefore, your index value "i" exceeds the number of elements in your collist list.

 

I suggest capturing the number of columns in dt outside the loop and using it as a fixed limit. 

path = "my file path with filename.jmp";

dt= open(path);
collist = dt <<get column names(string);
colname = Column("test1") <<getname;
colnumber = (Loc(collist,colname))[1];
num_col = N Cols (dt);

For(i=colnumber+1, i<=num_col,i++,  
   dt  <<new column("SG_"||collist[i], character);

);

Of note, I am unsure why the reverse loop works. You it should run into a similar issue with your collist variable.

Let us know if that solve your issue.

Best,

TS

 

 

 

Thierry R. Sornasse
dadawasozo
Level IV

Re: Subscript range issue when doing increasing for loop

Hi TS,

 

Yes, the error go away. However, I always use i<= N Cols (dt) and no issue at all. I dont understand why in this situation the same method failed?

Craige_Hales
Super User

Re: Subscript range issue when doing increasing for loop

@Thierry_S  the reverse loop captures ncols(dt) at the start of the loop, exactly once.

@dadawasozo 

  • the reverse loop isn't quite the same because it uses >colnumber+1 and probably should be >=colnumber+1 to match the non-working forward loop.
  • In the forward loop, ncols(dt) is re-evaluated each time it loops and keeps getting bigger, but collist is not getting bigger, so eventually the subscript fails.

I like @Thierry_S  solution.

 

Craige