Nested Loop Programming or Equivalent

Discussions and Tech Support related to the iMacros Firefox Add-on, including the built-in Javascript scripting interface for playing .js files.
Forum rules
Before asking a question or reporting an issue:
1. Please review the list of FAQ's.
2. Use the search box (at the top of each forum page) to see if a similar problem or question has already been addressed.
3. Try searching the iMacros Wiki - it contains the complete iMacros reference as well as plenty of samples and tutorials.
4. We can respond much faster to your posts if you include the following information: CLICK HERE FOR IMPORTANT INFORMATION TO INCLUDE IN YOUR POST
Post Reply
RayB
Posts: 30
Joined: Sat Mar 28, 2015 4:19 pm

Nested Loop Programming or Equivalent

Post by RayB » Thu Dec 05, 2019 7:35 pm

I am running Windows 7, Home Premium, with FF 61.0.1 (32-bit) and iMacros 10.0.2.1450. This is the free version of iMacros.

Hi,

I have searched the forums using Nested Loop with and without Modulo and am more confused than I was previously. :(

I have written this code to replace names associated with photo posts at a private web site (the reason for the *****'s):

Code: Select all

VERSION BUILD=8890130 RECORDER=FX
SET !ENCRYPTION NO
SET !TIMEOUT_PAGE 900
'  variable for user name 
SET !VAR2 "MissingUser"
SET !VAR3 {{!URLCURRENT}}
TAB T=1
URL GOTO={{!VAR3}}
TAB OPEN
TAB T=2
URL GOTO={{!VAR3}}
TAG POS={{!LOOP}} TYPE=IMG ATTR=ALT:*&&SRC:http://www.*******.net/gallery/data/*/thumbs/*&&TITLE:*&&CLASS:imgborder
TAG POS=1 TYPE=A ATTR=HREF:http://www.*******.net/gallery/editphoto.php?phoedit=*&&TITLE:Edit<SP>Photo
WAIT SECONDS=0.1
' next line replaces the username with VAR2
TAG POS=1 TYPE=INPUT:TEXT FORM=ACTION:http://www.*****.net/gallery/editphoto.php ATTR=TYPE:text&&NAME:newowner&&SIZE:50&&VALUE:*&&CLASS:bginput CONTENT={{!VAR2}}
' next line clicks the submit button
WAIT SECONDS=0.1
TAG POS=1 TYPE=INPUT:SUBMIT FORM=ACTION:http://www.******.net/gallery/editphoto.php ATTR=TYPE:submit&&VALUE:Submit<SP>changes&&CLASS:bginput
WAIT SECONDS=1
TAB T=2
TAB CLOSE
TAB T=1
URL GOTO={{!VAR3}}
Simply said, I edit the code to enter the username and then while on the website URL with 24 pictures on a page the position is currently set by the LOOP counter. The code works flawlessly. However when a page is done I have to manually click the NEXT button, wait until the new page completes and then restart the macro.

This code would click the Next button:

' next line increments the gallery page
TAG POS=1 TYPE=A ATTR=TXT:Next<SP>»

However I only want to go to the next page when LOOP = 24.

In other languages I would use an if statement but that is not found in iMacros. I am believing that using EVAL() might be used to only Tag the NEXT button, however, I can't figure out how to implement it.

Is this the best way to attempt the task I have or is there some other way that is better.

Thank you in advance for your help!
Last edited by RayB on Sat Dec 07, 2019 7:26 pm, edited 1 time in total.
chivracq
Posts: 8874
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Nested Loop Programming or Equivalent

Post by chivracq » Thu Dec 05, 2019 11:22 pm

RayB wrote:
Thu Dec 05, 2019 7:35 pm
I am running

Code: Select all

Windows 7, Home Premium, with FF 61.0.1 (32-bit) and iMacros 10.0.2.1450. 
This is the free version of iMacros.
Hi,

I have searched the forums using Nested Loop with and without Modulo and am more confused than I was previously. :(

I have written this code to replace names associated with photo posts at a private web site (the reason for the *****'s):

Code: Select all

VERSION BUILD=8890130 RECORDER=FX
SET !ENCRYPTION NO
SET !TIMEOUT_PAGE 900
'  variable for user name 
SET !VAR2 "MissingUser"
SET !VAR3 {{!URLCURRENT}}
TAB T=1
URL GOTO={{!VAR3}}
TAB OPEN
TAB T=2
URL GOTO={{!VAR3}}
TAG POS={{!LOOP}} TYPE=IMG ATTR=ALT:*&&SRC:http://www.*******.net/gallery/data/*/thumbs/*&&TITLE:*&&CLASS:imgborder
TAG POS=1 TYPE=A ATTR=HREF:http://www.*******.net/gallery/editphoto.php?phoedit=*&&TITLE:Edit<SP>Photo
WAIT SECONDS=0.1
' next line replaces the username with VAR2
TAG POS=1 TYPE=INPUT:TEXT FORM=ACTION:http://www.*****.net/gallery/editphoto.php ATTR=TYPE:text&&NAME:newowner&&SIZE:50&&VALUE:*&&CLASS:bginput CONTENT={{!VAR2}}
' next line clicks the submit button
WAIT SECONDS=0.1
TAG POS=1 TYPE=INPUT:SUBMIT FORM=ACTION:http://www.******.net/gallery/editphoto.php ATTR=TYPE:submit&&VALUE:Submit<SP>changes&&CLASS:bginput
WAIT SECONDS=1
TAB T=2
TAB CLOSE
TAB T=1
URL GOTO={{!VAR3}}
Simply said, I edit the code to enter the username and then while on the website URL with 24 pictures on a page the position is currently set by the LOOP counter. The code works flawlessly. However when a page is done I have to manually click the NEXT button, wait until the new page completes and then restart the macro.

This code would click the Next button:

Code: Select all

' next line increments the gallery page
TAG POS=1 TYPE=A ATTR=TXT:Next<SP>»
However I only want to go to the next page when LOOP = 24.

In other languages I would use an if statement but that is not found in iMacros. I am believing that using EVAL() might be used to only Tag the NEXT button, however, I can't figure out how to implement it.

Is this the best way to attempt the task I have or is there some other way that is better.

Thank you in advance for your help!

Qt not "really" specific to iMacros for FF... Thread belonged to the 'General' Sub-Forum... (where it might get moved...)

A bit "strange" that you staid at FF61, v10.0.2 for FF works with all current FF Versions... (FF[56-70+])

But yep, your Case is a "typical" Scenario with 'Nested Loops' + 'Modulo[24]'... 8)

=> When the Remainder is "0" you spit out a "1" ("0" otherwise) with 'EVAL()' to click on the 'Next' Button only on 'LOOP'=24,48,72,96. (You won't go any further with the 'Free' Version, ah-ah...! (Looping limited to 100...))
=> And for the Inner Loop for the 24 Pix per Page, you use the exact Remainder of 'Modulo[24]' to build your own Nested Loop, except when it is "=0" where you will need a "POS=24"...

=> That will give stg like...:

Code: Select all

'Inner Loop [1-24] for the 24 Pix per Page:
'TAG POS={{!LOOP}} TYPE=IMG ATTR=ALT:*&&SRC:http://www.*******.net/gallery/data/*/thumbs/*&&TITLE:*&&CLASS:imgborder
'>
SET !VAR1 EVAL("var n='{{!LOOP}}', m=24, x,y,z; x=n%m; if(x==0){z=m;} else{z=x;}; z;")
TAG POS={{!VAR1}} TYPE=IMG ATTR=ALT:*&&SRC:http://www.*******.net/gallery/data/*/thumbs/*&&TITLE:*&&CLASS:imgborder
'[...]
 
'Outer Loop => 'Modulo[24]' to go to Next Page:
'TAG POS=1 TYPE=A ATTR=TXT:Next<SP>»
'>
SET !ERRORIGNORE YES
SET !TIMEOUT_STEP 0
SET !VAR1 EVAL("var n='{{!LOOP}}', m=24, x,y,z; x=n%m; if(x==0){z=1;} else{z=0;}; z;")
TAG POS={{!VAR1}} TYPE=A ATTR=TXT:Next<SP>»
(Not tested...)

And no Pb to reuse the same '!VAR1' even if "both" in my Script have nothing related...
This is "Bad Practice" but is a bit "forced" by the stupid(*) Limitation where you can only use '!VAR[1-3]' with thee 'Free' Version(s), so you need to reuse the same Vars if you need more than 3... :wink:
(*) => "stupid" because it forces Users to write Ugly Code against all Best Practices and that Limitation can easily be bypassed, and I don't see Users squeezing many-many Vars in just 50 Lines of Code, ah-ah...! :roll:

EDIT: Typo in Script...: Missing '}' in "n='{{!LOOP}}'", x2... [Corrected...]
Last edited by chivracq on Sat Dec 07, 2019 5:03 pm, edited 1 time in total.
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE').
- I don't even read the Qt if that (required) Info is not mentioned...!
- Script & URL help a lot for more "educated" Help...
RayB
Posts: 30
Joined: Sat Mar 28, 2015 4:19 pm

Re: Nested Loop Programming or Equivalent

Post by RayB » Sat Dec 07, 2019 3:38 pm

Hello chivracq,

Thank You, I really appreciate your help!! I still don't fully understand how the code works, but I am learning at a high rate.

I discovered that the original code you provided was missing a } on the variable var n='{{!LOOP}' , that is it should have been var n='{{!LOOP}}' . But that was a good learning for me!

Here is the code as it is now.

Code: Select all

VERSION BUILD=10021450
SET !ENCRYPTION NO
SET !TIMEOUT_PAGE 900
'  variable for user name 
SET !VAR2 "Missing User"
SET !VAR3 {{!URLCURRENT}}
TAB T=1
URL GOTO={{!VAR3}}
TAB OPEN
TAB T=2
URL GOTO={{!VAR3}}
'Inner Loop [1-24] for the 24 Pix per Page:

SET !VAR1 EVAL("var n='{{!LOOP}}', m=24, x,y,z; x=n%m; if(x==0){z=m;} else{z=x;}; z;")

TAG POS={{!LOOP}} TYPE=IMG ATTR=ALT:*&&SRC:http://www.*****.net/gallery/data/*/thumbs/*&&TITLE:*&&CLASS:imgborder
TAG POS=1 TYPE=A ATTR=HREF:http://www.******.net/gallery/editphoto.php?phoedit=*&&TITLE:Edit<SP>Photo
WAIT SECONDS=0.1
' next line replaces the username with VAR2
TAG POS=1 TYPE=INPUT:TEXT FORM=ACTION:http://www.*****.net/gallery/editphoto.php ATTR=TYPE:text&&NAME:newowner&&SIZE:50&&VALUE:*&&CLASS:bginput CONTENT={{!VAR2}}
' next line clicks the submit button
WAIT SECONDS=0.1
TAG POS=1 TYPE=INPUT:SUBMIT FORM=ACTION:http://www.*****.net/gallery/editphoto.php ATTR=TYPE:submit&&VALUE:Submit<SP>changes&&CLASS:bginput
WAIT SECONDS=1
TAB T=2
TAB CLOSE
TAB T=1
URL GOTO={{!VAR3}}

'Outer Loop => 'Modulo[24]' to go to Next Page:
SET !ERRORIGNORE YES
SET !TIMEOUT_STEP 0
SET !VAR1 EVAL("var n='{{!LOOP}}', m=24, x,y,z; x=n%m; if(x==0){z=1;} else{z=0;}; z;")
' next line increments the gallery page if loop = 24
TAG POS={{!VAR1}} TYPE=A ATTR=TXT:Next<SP>»

I run this code by setting the Play macro repeateatedly: Max to 99 as you would expect.

This code works on the first page and then does nothing except to move forward 3 pages. Here is what I believe is happening. After the first page is completed, !LOOP = 24 so the code clicks the Next entry which moves to the next page. At this point !Loop = 25. Since there are only 24 entries on a page nothing happens, but the macro continues to run until reaching 'LOOP'=48,72,96 where it advances a page. So now the issue becomes how to either reset the !LOOP to 1 since it is being used for the position on the page. However we are out of variables and the documentation says "The command to explicitly set the value of !LOOP is only executed once"

So I am looking for advice.

Thank you again!
Last edited by RayB on Sat Dec 07, 2019 7:25 pm, edited 2 times in total.
chivracq
Posts: 8874
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Nested Loop Programming or Equivalent

Post by chivracq » Sat Dec 07, 2019 5:17 pm

RayB wrote:
Sat Dec 07, 2019 3:38 pm
Hello chivracq,

Thank You, I really appreciate your help!! I still don't fully understand how the code works, but I am learning at a high rate.

I discovered that the original code you provided was missing a } on the variable var n='{{!LOOP}' , that is it should have been var n='{{!LOOP}}' . But that was a good learning for me!

Here is the code as it is now.

Code: Select all

VERSION BUILD=10021450
SET !ENCRYPTION NO
SET !TIMEOUT_PAGE 900
'  variable for user name 
SET !VAR2 "Missing User"
SET !VAR3 {{!URLCURRENT}}
TAB T=1
URL GOTO={{!VAR3}}
TAB OPEN
TAB T=2
URL GOTO={{!VAR3}}
'Inner Loop [1-24] for the 24 Pix per Page:

SET !VAR1 EVAL("var n='{{!LOOP}}', m=24, x,y,z; x=n%m; if(x==0){z=m;} else{z=x;}; z;")

TAG POS={{!LOOP}} TYPE=IMG ATTR=ALT:*&&SRC:http://www.*****.net/gallery/data/*/thumbs/*&&TITLE:*&&CLASS:imgborder
TAG POS=1 TYPE=A ATTR=HREF:http://www.******.net/gallery/editphoto.php?phoedit=*&&TITLE:Edit<SP>Photo
WAIT SECONDS=0.1
' next line replaces the username with VAR2
TAG POS=1 TYPE=INPUT:TEXT FORM=ACTION:http://www.*****.net/gallery/editphoto.php ATTR=TYPE:text&&NAME:newowner&&SIZE:50&&VALUE:*&&CLASS:bginput CONTENT={{!VAR2}}
' next line clicks the submit button
WAIT SECONDS=0.1
TAG POS=1 TYPE=INPUT:SUBMIT FORM=ACTION:http://www.*****.net/gallery/editphoto.php ATTR=TYPE:submit&&VALUE:Submit<SP>changes&&CLASS:bginput
WAIT SECONDS=1
TAB T=2
TAB CLOSE
TAB T=1
URL GOTO={{!VAR3}}

'Outer Loop => 'Modulo[24]' to go to Next Page:
SET !ERRORIGNORE YES
SET !TIMEOUT_STEP 0
SET !VAR1 EVAL("var n='{{!LOOP}', m=24, x,y,z; x=n%m; if(x==0){z=1;} else{z=0;}; z;")
' next line increments the gallery page if loop = 24
TAG POS={{!VAR1}} TYPE=A ATTR=TXT:Next<SP>»
I run this code by setting the Play macro repeateatedly: Max to 99 as you would expect.

This code works on the first page and then does nothing except to move forward 3 pages. Here is what I believe is happening. After the first page is completed, !LOOP = 24 so the code clicks the Next entry which moves to the next page. At this point !Loop = 25. Since there are only 24 entries on a page nothing happens, but the macro continues to run until reaching 'LOOP'=48,72,96 where it advances a page. So now the issue becomes how to either reset the !LOOP to 1 since it is being used for the position on the page. However we are out of variables and the documentation says "The command to explicitly set the value of !LOOP is only executed once"

So I am looking for advice.

Thank you again!

Oh...!, yep indeed for the Typo, I've corrected it (x2)... :P
I wrote the Script from Scratch, without Testing it, like I had mentioned... :wink:

Notice the ']CODE[' Forum Tags to format your Scripts in your Posts, that would make the Thread a bit easier to read... :idea:

But for the (Inner) Looping, and the Behaviour you describe, then yep, normal...:
You still use "your" "TAG POS={{!LOOP}}" for the Inner Loop, instead of using "my" "TAG POS={{!VAR1}}"... :!:
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE').
- I don't even read the Qt if that (required) Info is not mentioned...!
- Script & URL help a lot for more "educated" Help...
RayB
Posts: 30
Joined: Sat Mar 28, 2015 4:19 pm

Re: Nested Loop Programming or Equivalent

Post by RayB » Sun Dec 08, 2019 4:23 pm

Hi chivracq,

Thank you so much for helping create this code. It works flawlessly! I finally figured out how to display the Code in the posts and as you say it makes it easier to read.
chivracq
Posts: 8874
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Nested Loop Programming or Equivalent

Post by chivracq » Sun Dec 08, 2019 8:15 pm

RayB wrote:
Sun Dec 08, 2019 4:23 pm
Hi chivracq,

Thank you so much for helping create this code. It works flawlessly! I finally figured out how to display the Code in the posts and as you say it makes it easier to read.

Yep, I saw you adding the ']CODE[' Tags yesterday IRT, and I'm glad the Script works fine... :D
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE').
- I don't even read the Qt if that (required) Info is not mentioned...!
- Script & URL help a lot for more "educated" Help...
Post Reply