i=i+1 does not increment

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
horscht13
Newbie
Posts: 6
Joined: Sun Dec 21, 2008 12:11 pm

i=i+1 does not increment

Post by horscht13 » Sun Dec 21, 2008 12:20 pm

Hi all,

I have a problem with the repeat-until routine.
I have something like this:

Let>i=0
repeat>i
Let>i=i+1
//Do something
until>i>100

If I have the loop running without much in it, everything works fine. If the loop takes longer (in my case one iteration would take about 0.5hour with long waits in it), the Let>i=i+1 is ignored. If I set a breakpoint just before the increment-line and go step-by-step, I can see that the first iteration is correct, i=1. But next time i stays at 1.
Somebody has an idea?

Thanks...
Regards horscht

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Sun Dec 21, 2008 1:38 pm

That code works fine for me. I think we need to see the rest of your code. Quite likely there is something else in your code which is changing the value of i. Use Tools/Variable Explorer to see where i is being set.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

armsys
Automation Wizard
Posts: 1108
Joined: Wed Dec 04, 2002 10:28 am
Location: Hong Kong

Post by armsys » Sun Dec 21, 2008 1:43 pm

Hi horscht,
Your script looks fine. The crucial question is what's inside the //Do something.

horscht13
Newbie
Posts: 6
Joined: Sun Dec 21, 2008 12:11 pm

Post by horscht13 » Sun Dec 21, 2008 2:01 pm

So here´s the code. i is change nowhere. It´s just used to select the line to read in the first file.
With the breakpoint at the end (right before the until) I can see, that i is still one after the first looping and going step by step through the first lines of the repeat-loop, i just stays one.

Code: Select all

Let>WW_TIMEOUT=5
CapsOff
/**BREAKPOINT**/

Let>j=0
Repeat>j
//**BREAKPOINT**
    Add>j=1
    //Read a line in sws.txt and write it to new.txt
    ReadLn>C:\somepath\somefile.txt,j,SWSstrLine
    If>SWSstrLine=##EOF##,finish
        //Message>SWSstrLine
        WriteLn>C:\somepath\new.txt,1,SWSstrLine
    Endif
Until>j>100
Edit: I have deleted the parts of the code, that didn´t matter for the solution of my problem...
Last edited by horscht13 on Sun Dec 21, 2008 2:37 pm, edited 1 time in total.

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Sun Dec 21, 2008 2:07 pm

Actually your loop counter is j not i.

And you're trying to increment it with the Add command. But you have the syntax wrong. You have:

Add>j=1

You should have:

Add>j,1

Or:

Let>j=j+1
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

horscht13
Newbie
Posts: 6
Joined: Sun Dec 21, 2008 12:11 pm

Post by horscht13 » Sun Dec 21, 2008 2:20 pm

Ok, sorry. Obviously that was no working version. I just tried different things to get it to work. (Changing i to j and using the add command were some of them)

I now figured out, that as long as there is no i,j,k or whatever in the ReadLn it works. But if there is (as is in Line 10) the increment command just doesn´t work. Can someone confirm this? Somebody a solution?

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Sun Dec 21, 2008 2:29 pm

Actually I see the problem. It is your WriteLn line. You have specified 1 as the return variable. So you are creating a variable called 1, set to whatever the result of the WriteLn command is. This is probably zero (success). So 1 becomes 0. You say j=j+1. j starts off as 1. 1 then becomes zero. 1+0 is 1. So j is set to 1 in every iteration.

Replace the 1 in WriteLn with a sensible variable name.

The problem is not with your ReadLn line.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

horscht13
Newbie
Posts: 6
Joined: Sun Dec 21, 2008 12:11 pm

Post by horscht13 » Sun Dec 21, 2008 2:35 pm

Thanks very much. I think that was it!!!
I just assumed, that WriteLn works as ReadLn.

best regards horscht

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Sun Dec 21, 2008 2:37 pm

ReadLn reads a line from a file and therefore is given the line number to read. The second parm is the line number.

WriteLn writes a line to the END of a file. The second parm is the variable to store the result of the operation.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

horscht13
Newbie
Posts: 6
Joined: Sun Dec 21, 2008 12:11 pm

Post by horscht13 » Sun Dec 21, 2008 2:39 pm

:roll: that was 2 days :roll:

Anyway: Thanks very much!!

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Sun Dec 21, 2008 3:05 pm

2 days? 2 hours according to the forum timestamp.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Sun Dec 21, 2008 3:27 pm

A general guideline is not to use variables of only 1 character, it is too easy to have other values assigned to them by mistake. I usually name most of my variables with a meaningful name, starting with lowercase "v" for variable for easy identification.

Let>vLoop1=0
Repeat>vLoop1
//**BREAKPOINT**
Add>vLoop1,1
//Read a line in sws.txt and write it to new.txt
ReadLn>C:\somepath\somefile.txt,%vLoop1%,SWSstrLine
If>SWSstrLine=##EOF##,finish
//Message>SWSstrLine
WriteLn>C:\somepath\new.txt,vLine1,SWSstrLine
Endif
Until>vLoop1>100

Note that I usually surround my variables in formulas/commands with "%" so that I can see the actual value in the log files. But do not use %vVariable% format when assigning a value to it.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Sun Dec 21, 2008 3:31 pm

You can also use VAREXPLICIT to avoid issues like this.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

horscht13
Newbie
Posts: 6
Joined: Sun Dec 21, 2008 12:11 pm

Post by horscht13 » Sun Dec 21, 2008 4:41 pm

mtettmar wrote:2 days? 2 hours according to the forum timestamp.
No, I ment for me to solve it...
It´s always the small bugs that keep you. :?

Will see about changing my v-names.

Regards

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts