The Origin Forum
File Exchange
Try Origin for Free
The Origin Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ | Send File to Tech support
Username:
Password:
Save Password
Forgot your Password? | Admin Options

 All Forums
 Origin Forum for Programming
 LabTalk Forum
 Nesting a loop inside of another loop
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

Smilik305

Germany
6 Posts

Posted - 09/13/2017 :  11:23:05 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Hello!
I am trying to make a script with which I can divide the first column and then every third column by a value from the column of another workbook, so that the first column will be divided by the first value of the column, then the forth column by its second value and so on. The result must then be inserted into the third workbook, which is just a copy of the first workbook, but the first and then every third columns are changed accordingly. Here is the script I came up with:

int aa;
int ii;
int N=10;
int NN=1+(N-1)*3;

for (aa=1; aa <= NN; aa += 3)
{
range xx=[Book1]sheet1!col($(aa));
range zz=[Book3]sheet1!col($(aa));
loop (ii, 1, N) {
range yy=[Book2]sheet1!cell($(ii),2);
};
zz=xx/yy;
};

It seems that the internal "loop" loop does not work. Could you please help me how I can nest a loop inside another one, so that the aforementioned desired logics holds.


Edited by - Smilik305 on 09/13/2017 11:31:52 AM

nick_n

Finland
125 Posts

Posted - 09/13/2017 :  2:21:27 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi,

I didn't try your script but I can see that it must be the problem with range declaration. Be cureful when declare your range, I mean put range out of local scope {}, range declared inside of {} will work only inside. See more: http://www.originlab.com/doc/LabTalk/guide/Data-Types-and-vars#Scope_of_Variables

Nikolay
Go to Top of Page

nick_n

Finland
125 Posts

Posted - 09/13/2017 :  3:01:04 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
However, your case is easy to fix, just replace your:
range yy=[Book2]sheet1!cell($(ii),2);

by:
range yy=[Book2]sheet1!2;
double dYY=yy[ii];
...
...
zz=xx/dYY;

Usually, I'm using subrange like:
range rr=[Book1]Sheet1; //declared outside

loop(aa, 1, 2)
{
range rr1=%(rr)!col($(aa)); //subrange declared inside of scope
...

}

Best,

Nikolay
Go to Top of Page

nick_n

Finland
125 Posts

Posted - 09/13/2017 :  3:13:11 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Sorry, replace square brackets with:
dYY=yy(ii);

Nikolay
Go to Top of Page

Smilik305

Germany
6 Posts

Posted - 09/14/2017 :  09:51:44 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hello Nikolay,
Thank you for the fast reply!
I modified the code as you suggested:

int aa;
int ii;
int N=10;
int NN=1+(N-1)*3;

for (aa=1; aa <= NN; aa += 3)
{
range xx=[Book1]sheet1!col($(aa));
range zz=[Book3]sheet1!col($(aa));
loop (ii, 1, N) {
range yy=[Book2]sheet1!2;
double dYY=yy(ii);
zz=xx/dYY;//if this expression goes after the curly bracket, the
result does not change.
};
};

It got better, but now all the columns of the Book1 are divided by the same first value from the column 2 of the Book2, and I need that each next column from the Book1 is divided by the next value from the column 2 of the Book2. In other words, one needs that the "aa" and "ii" counters change simultaneously after each run through the script. However, "ii" remains 1 all the time. Do you know how to do that?
Go to Top of Page

nick_n

Finland
125 Posts

Posted - 09/14/2017 :  2:03:34 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi, I didn't catch script idea yesterday.
Hope that could help you:

int ii, iCount=0;
window -a book1; //source book is active now
worksheet -da book3; //makes duplicate of original data to book3

loop(ii, 1, wks.nCols)
{
double tmp1=(ii-1)/3;
int tmp2=(ii-1)/3;
if (tmp1==tmp2) //every third column in book1
{ //will be replaced with new value
iCount++;
range xx=[Book3]sheet1!col($(ii));
range yy=[Book2]sheet1!2;
double dYY=yy(iCount);
xx=xx/dYY;
}
}


Nikolay
Go to Top of Page

Smilik305

Germany
6 Posts

Posted - 09/15/2017 :  06:27:49 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hello Nikolay,

Yes, it works. Thank you very much, especially for the trick with iCount++. Also, it's important to make a column 2 to be x.

Edited by - Smilik305 on 09/15/2017 06:29:08 AM
Go to Top of Page
  Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000