Jump to content

Loopy Advice Wanted


czardas
 Share

Recommended Posts

As an amatuer I have only used 'Do Until' and 'While WEnd' loops in my scripts. These seem quite similar to me. I seem to have had difficulty going more than two levels in. I have not been able to create a loop within a loop within a loop. I have only been able to create a loop within a loop. This is likely to be because of bad arguments. I would like to know if there is a preffered syntax for this type of looping, and how many levels in can you go. I would expect that loops could created within loops indefinately so I must be doing something wrong.

Link to comment
Share on other sites

oops - left my crystal ball in the tent. Mind giving us some code you have previously tried, and maybe we can tell you why it isn't working.

The cake is a lie.www.theguy0000.com is currentlyUP images.theguy0000.com is currentlyUP all other *.theguy0000.com sites are DOWN

Link to comment
Share on other sites

As an amatuer I have only used 'Do Until' and 'While WEnd' loops in my scripts. These seem quite similar to me. I seem to have had difficulty going more than two levels in. I have not been able to create a loop within a loop within a loop. I have only been able to create a loop within a loop. This is likely to be because of bad arguments. I would like to know if there is a preffered syntax for this type of looping, and how many levels in can you go. I would expect that loops could created within loops indefinately so I must be doing something wrong.

You'll be able to nest loops deeper than you'll ever need to go.

Here's a quick example of a 3-layer loop setup with While:

; 3 degrees of nesting

Global $x = 0, $y = 0, $z = 0

While 1
    
    $x = $x + 1
    If $x > 5 Then ExitLoop
    ConsoleWrite("We're on 1st iteration " & $x & " of 5" & @CRLF)
    
    While 1
        
        $y = $y + 1
        If $y > 5 Then ExitLoop
        ConsoleWrite("We're on 2nd iteration " & $y & " of 5" & @CRLF)
        
        While 1
            
            $z = $z + 1
            If $z > 5 Then ExitLoop
            ConsoleWrite("We're on 3rd iteration " & $z & " of 5" & @CRLF)
        WEnd
        $z = 0
                
    WEnd
    $y = 0
    
WEnd

Can you post an example of what you're trying to accomplish?

-S

Edit: whoops, someone else already asked while I was posting.

Edited by Locodarwin
(Yet Another) ExcelCOM UDF"A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly...[indent]...specialization is for insects." - R. A. Heinlein[/indent]
Link to comment
Share on other sites

You'll be able to nest loops deeper than you'll ever need to go.

Here's a quick example of a 3-layer loop setup with While:

; 3 degrees of nesting

Global $x = 0, $y = 0, $z = 0

While 1
    
    $x = $x + 1
    If $x > 5 Then ExitLoop
    ConsoleWrite("We're on 1st iteration " & $x & " of 5" & @CRLF)
    
    While 1
        
        $y = $y + 1
        If $y > 5 Then ExitLoop
        ConsoleWrite("We're on 2nd iteration " & $y & " of 5" & @CRLF)
        
        While 1
            
            $z = $z + 1
            If $z > 5 Then ExitLoop
            ConsoleWrite("We're on 3rd iteration " & $z & " of 5" & @CRLF)
        WEnd
        $z = 0
                
    WEnd
    $y = 0
    
WEnd

Can you post an example of what you're trying to accomplish?

-S

Edit: whoops, someone else already asked while I was posting.

This is just what I'm after, a clear example. Thanks.

Edited by czardas
Link to comment
Share on other sites

How do I post a large script? I'm not sure if it will appear correctly.

CODE: AutoIt

Opt("SendKeyDelay", 10)

$Year = InputBox ("START DATE", "Enter the year (4 figures)", "")

$Month = InputBox ("START DATE", "Enter the month 01 - 12", "")

Run("notepad.exe")

WinWaitActive("Untitled - Notepad")

Send ($Month)

Send ("/")

Send ($Year)

$MMYYYY = WinGetText("Untitled - Notepad")

WinClose("Untitled - Notepad")

WinWaitActive("Notepad", "The text in the Untitled file has changed")

Send("!n")

Send ("{LWINDOWN}r{LWINUP}")

WinWaitActive("Run","")

Send (@MyDocumentsDir&"\New Ledger.xls")

Send ("{ENTER}")

WinWaitActive ("Microsoft Excel - New Ledger.xls")

; Group Students (Add New Student)

$date = InputBox ("GROUP LESSONS", "Enter Date 01 - 31", "")

While $date > 0

$price = InputBox ("GROUP LESSONS", "Enter the lesson price.", "")

$answer = MsgBox (4, "ADD STUDENTS", "Do you want to add a new student to the group?")

If $answer = 7 Then

Else

Send ("{CTRLDOWN}{PGDN 4}{CTRLUP}")

Sleep (300)

$text = InputBox ("ADD STUDENTS", "Enter the student's name", "")

Do

;Added start

Sleep (300)

Send ("{CTRLDOWN}f{CTRLUP}")

WinWaitActive ("Find", "")

Send ($text)

Send ("{ENTER}")

Sleep (300)

If WinExists ("Microsoft Excel", "Microsoft Excel cannot find the data you're searching for.",) Then

Send ("{ENTER}")

Send ("{ALTDOWN}{F4}{ALTUP}")

Sleep (100)

Send ("{CTRLDOWN}{PGDN}{CTRLUP}")

Sleep (100)

Send ("{CTRLDOWN}f{CTRLUP}")

WinWaitActive ("Find", "")

Send ($text)

Send ("{ENTER}")

Sleep (300)

If WinExists ("Microsoft Excel", "Microsoft Excel cannot find the data you're searching for.",) Then

Send ("{ENTER}")

Send ("{ALTDOWN}{F4}{ALTUP}")

Sleep (100)

Send ("{CTRLDOWN}{PGUP}{CTRLUP}")

Sleep (100)

Send ("{CTRLDOWN}{HOME}{CTRLUP}{DOWN}")

Sleep (300)

Send("{ALTDOWN}ir{ALTUP}")

Sleep (500)

Send ($text)

Sleep (300)

$missed = InputBox ("ADD STUDENTS", "How many lessons has "&($text)&" missed?", "")

Send ("{RIGHT}")

Send ($missed*$price)

Sleep (300)

Send ("{RIGHT}0")

Sleep (300)

Send ("{CTRLDOWN}{HOME}{CTRLUP}")

Else

Send ("{ALTDOWN}{F4}{ALTUP}")

Sleep (100)

$answer = MsgBox (4,"WARNING",$text&" already exists. Do you to move "&$text&"?")

Sleep (100)

If $answer = 6 Then

;delete totals before removing student

Send ("{CTRLDOWN}{END}{CTRLUP}{DEL}")

Sleep (300)

Send ("{LEFT}{DEL}")

Sleep (300)

Send ("{CTRLDOWN}f{CTRLUP}")

WinWaitActive ("Find", "")

Send ($text)

Sleep (300)

Send ("{ENTER}")

Send ("{ALTDOWN}{F4}{ALTUP}")

Sleep (100)

;totals deleted

Send ("{RIGHT}")

Sleep (300)

Send ("{CTRLDOWN}c{CTRLUP}")

Sleep (300)

$bak1 = ClipGet()

Sleep (300)

If $bak1 + 0 = 0 Then

Send ("{RIGHT}")

Sleep (300)

Send ("{CTRLDOWN}c{CTRLUP}")

Sleep (300)

$bak2 = ClipGet()

Sleep (300)

; delete the sudent before the argument

Send ("{ALTDOWN}edr{ALTUP}{ENTER}")

Sleep (300)

Send ("{CTRLDOWN}{END}{CTRLUP}")

Sleep (300)

Send ("{CTRLDOWN}{SPACE}{CTRLUP}")

Send("{ALTDOWN}={ALTUP}")

Sleep (300)

Send ("{LEFT}")

Sleep (300)

Send ("{CTRLDOWN}{SPACE}{CTRLUP}")

Send("{ALTDOWN}={ALTUP}")

Sleep (300)

Send ("{CTRLDOWN}{HOME}{CTRLUP}")

Sleep (100)

Send ("{CTRLDOWN}{PGUP}{CTRLUP}")

Sleep (100)

; Now add the student

Send ("{CTRLDOWN}{HOME}{CTRLUP}{DOWN}")

Sleep (300)

Send("{ALTDOWN}ir{ALTUP}")

Sleep (500)

Send ($text)

Sleep (300)

; student added

$missed = InputBox ("ADD STUDENTS", "How many lessons has "&($text)&" missed?", "")

Sleep (100)

If $bak2 + 0 = 0 Then

Send ("{RIGHT}")

Send ($missed*$price)

Sleep (300)

Send ("{RIGHT}0")

Sleep (300)

Else

If $bak2 + 0 > $missed*$price then

Send ("{RIGHT}0{RIGHT}")

Sleep (300)

Send ($bak2 - $missed*$price)

Else

Send ("{RIGHT}")

Send ($missed*$price - $bak2 )

Sleep (300)

Send ("{RIGHT}0")

Sleep (300)

EndIf

EndIf

; calculate totals

Send ("{CTRLDOWN}{END}{CTRLUP}{DEL}")

Sleep (300)

Send ("{CTRLDOWN}{SPACE}{CTRLUP}")

Send("{ALTDOWN}={ALTUP}")

Sleep (300)

Send ("{LEFT}{DEL}")

Sleep (300)

Send ("{CTRLDOWN}{SPACE}{CTRLUP}")

Send("{ALTDOWN}={ALTUP}")

Sleep (300)

Send ("{CTRLDOWN}{HOME}{CTRLUP}")

Else

; delete the sudent

Send ("{ALTDOWN}edr{ALTUP}{ENTER}")

Sleep (300)

Send ("{CTRLDOWN}{END}{CTRLUP}")

Sleep (300)

Send ("{CTRLDOWN}{SPACE}{CTRLUP}")

Send("{ALTDOWN}={ALTUP}")

Sleep (300)

Send ("{LEFT}")

Sleep (300)

Send ("{CTRLDOWN}{SPACE}{CTRLUP}")

Send("{ALTDOWN}={ALTUP}")

Sleep (300)

Send ("{CTRLDOWN}{HOME}{CTRLUP}")

Sleep (100)

Send ("{CTRLDOWN}{PGUP}{CTRLUP}")

Sleep (100)

; Now add the student

Send ("{CTRLDOWN}{HOME}{CTRLUP}{DOWN}")

Sleep (300)

Send("{ALTDOWN}ir{ALTUP}")

Sleep (500)

Send ($text)

Sleep (300)

; student added

$missed = InputBox ("ADD STUDENTS", "How many lessons has "&($text)&" missed?", "")

Sleep (100)

Send ("{RIGHT}")

Sleep (300)

Send ($bak1 + $missed*$price)

Sleep (300)

Send ("{RIGHT}0")

Sleep (300)

; calculate totals

Send ("{CTRLDOWN}{END}{CTRLUP}{DEL}")

Sleep (300)

Send ("{CTRLDOWN}{SPACE}{CTRLUP}")

Send("{ALTDOWN}={ALTUP}")

Sleep (300)

Send ("{LEFT}{DEL}")

Sleep (300)

Send ("{CTRLDOWN}{SPACE}{CTRLUP}")

Send("{ALTDOWN}={ALTUP}")

Sleep (300)

Send ("{CTRLDOWN}{HOME}{CTRLUP}")

Sleep (100)

EndIf

Else

Send ("{CTRLDOWN}{HOME}{CTRLUP}")

Sleep (100)

Send ("{CTRLDOWN}{PGUP}{CTRLUP}")

Sleep (100)

$missed = 0

EndIf

EndIf

Else

Send ("{ALTDOWN}{F4}{ALTUP}")

Sleep (100)

MsgBox (0, "ERROR", ($text)&" is already a member of the group.")

Sleep (100)

Send ("{CTRLDOWN}{HOME}{CTRLUP}")

Sleep (100)

$missed = 0

EndIf

; END OF INSERTION

If $missed > 0 Then

Send ("{CTRLDOWN}{PGUP 3}{CTRLUP}")

Sleep (100)

Send("{CTRLDOWN}{END}{CTRLUP}")

Send ("{DOWN}{HOME}")

Send ($date&"/"&$MMYYYY)

Sleep (100)

Send ("{RIGHT}")

Send ($text)

Send (" - Late Enrolment.")

Send ("{RIGHT 3}")

Send ($missed*$price)

Sleep (100)

Send ("{RIGHT 3}")

Send ($missed*$price)

Sleep (100)

Send ("{HOME}")

Sleep (100)

Send ("{CTRLDOWN}{PGDN 3}{CTRLUP}")

Else

EndIf

Sleep (100)

$text = InputBox ("ADD STUDENTS", "Enter the student's name", "")

$len = StringLen($text)

Until $len = 0

Send ("{CTRLDOWN}{END}{CTRLUP}{DEL}")

Sleep (300)

Send ("{CTRLDOWN}{SPACE}{CTRLUP}")

Send("{ALTDOWN}={ALTUP}")

Send ("{LEFT}{DEL}")

Sleep (300)

Send ("{CTRLDOWN}{SPACE}{CTRLUP}")

Send("{ALTDOWN}={ALTUP}")

Send ("{CTRLDOWN}{HOME}{CTRLUP}")

Sleep (100)

Send ("{CTRLDOWN}{PGUP 4}{CTRLUP}")

EndIf

; Group Students (Delete Existing Student)

$answer = MsgBox (4, "DELETE STUDENTS", "Do you want to delete a student from the group?")

If $answer = 7 Then

Else

Send ("{CTRLDOWN}{PGDN 4}{CTRLUP}")

Sleep (300)

$text = InputBox ("DELETE STUDENTS", "Enter the student's name", "")

Do

Send ("{CTRLDOWN}{END}{CTRLUP}{DEL}")

Sleep (300)

Send ("{LEFT}{DEL}")

Sleep (300)

Send ("{CTRLDOWN}f{CTRLUP}")

WinWaitActive ("Find", "")

Send ($text)

Send ("{ENTER}")

Sleep (300)

If WinExists ("Microsoft Excel", "Microsoft Excel cannot find the data you're searching for.",) Then

Send ("{ENTER}")

Send ("{ALTDOWN}{F4}{ALTUP}")

Sleep (300)

MsgBox (0,"ERROR", "The name does not appear to be in the list.")

Sleep (300)

Send ("{CTRLDOWN}{SPACE}{CTRLUP}")

Send("{ALTDOWN}={ALTUP}")

Sleep (300)

Send ("{RIGHT}")

Sleep (300)

Send ("{CTRLDOWN}{SPACE}{CTRLUP}")

Send("{ALTDOWN}={ALTUP}")

Send ("{CTRLDOWN}{HOME}{CTRLUP}")

Else

Send ("{ENTER}{TAB}{ENTER}")

Sleep (300)

Send ("{RIGHT}")

Sleep (300)

Send ("{CTRLDOWN}c{CTRLUP}")

Sleep (300)

$bak = ClipGet()

If $bak = 0 Then

Send ("{RIGHT}")

Sleep (300)

Send ("{CTRLDOWN}c{CTRLUP}")

Sleep (300)

$bak = ClipGet()

If $bak = 0 Then

Else

Send ("{CTRLDOWN}{PGDN}{CTRLUP}")

Sleep (300)

Send ("{CTRLDOWN}{HOME}{CTRLUP}{DOWN}")

Sleep (300)

Send("{ALTDOWN}ir{ALTUP}")

Sleep (500)

Send ($text)

Sleep (300)

Send ("{RIGHT}0{RIGHT}")

Sleep (300)

Send ($bak)

Sleep (300)

Send ("{CTRLDOWN}{END}{CTRLUP}{DEL}")

Sleep (300)

Send ("{CTRLDOWN}{SPACE}{CTRLUP}")

Send("{ALTDOWN}={ALTUP}")

Send ("{LEFT}{DEL}")

Sleep (300)

Send ("{CTRLDOWN}{SPACE}{CTRLUP}")

Send("{ALTDOWN}={ALTUP}")

Send ("{CTRLDOWN}{HOME}{CTRLUP}")

Send ("{CTRLDOWN}{PGUP}{CTRLUP}")

EndIf

Else

Send ("{CTRLDOWN}{PGDN}{CTRLUP}")

Sleep (300)

Send ("{CTRLDOWN}{HOME}{CTRLUP}{DOWN}")

Sleep (300)

Send("{ALTDOWN}ir{ALTUP}")

Sleep (500)

Send ($text)

Sleep (300)

Send ("{RIGHT}")

Sleep (300)

Send ($bak)

Sleep (300)

Send ("{CTRLDOWN}{HOME}{CTRLUP}{DOWN}{RIGHT 2}")

Sleep (300)

Send ("0")

Sleep (300)

Send ("{CTRLDOWN}{END}{CTRLUP}{DEL}")

Sleep (300)

Send ("{CTRLDOWN}{SPACE}{CTRLUP}")

Send("{ALTDOWN}={ALTUP}")

Sleep (300)

Send ("{LEFT}{DEL}")

Sleep (300)

Send ("{CTRLDOWN}{SPACE}{CTRLUP}")

Send("{ALTDOWN}={ALTUP}")

Sleep (300)

Send ("{CTRLDOWN}{HOME}{CTRLUP}")

Send ("{CTRLDOWN}{PGUP}{CTRLUP}")

EndIf

Sleep (300)

Send ("{ALTDOWN}edr{ALTUP}{ENTER}")

Sleep (300)

Send ("{CTRLDOWN}{END}{CTRLUP}")

Sleep (300)

Send ("{CTRLDOWN}{SPACE}{CTRLUP}")

Send("{ALTDOWN}={ALTUP}")

Sleep (300)

Send ("{LEFT}")

Sleep (300)

Send ("{CTRLDOWN}{SPACE}{CTRLUP}")

Send("{ALTDOWN}={ALTUP}")

Send ("{CTRLDOWN}{HOME}{CTRLUP}")

EndIf

Sleep (300)

$text = InputBox ("DELETE STUDENTS", "Enter the student's name", "")

$len = StringLen($text)

Until $len = 0

Sleep (300)

Send("{CTRLDOWN}{HOME}{CTRLUP}")

Send ("{CTRLDOWN}{PGUP 4}{CTRLUP}")

EndIf

Sleep (300)

Send ("{CTRLDOWN}{PGDN 4}{CTRLUP}")

$count = -1

Do

Sleep (100)

Send ("{DOWN}")

Send ("{CTRLDOWN}c{CTRLUP}")

$bak = ClipGet()

$len = StringLen($bak)

$count = $count + 1

Until $len = 2

; Adjust groups debit/credit

Send("{CTRLDOWN}{HOME}{CTRLUP}")

$number = 0

Do

Send ("{RIGHT 2}{DOWN}")

Sleep (300)

Send ("{CTRLDOWN}c{CTRLUP}")

Sleep (300)

$bak1 = ClipGet()

Sleep (300)

If $bak1 - $price < 0 Then

Send ("{DEL}0{LEFT}")

Sleep (300)

Send ("{CTRLDOWN}c{CTRLUP}")

Sleep (300)

$bak2 = ClipGet()

Sleep (300)

Send ($price + $bak2 - $bak1)

Else

Send ($bak1 - $price)

Sleep (300)

EndIf

Send ("{HOME}")

$number = $number + 1

Until $number = $count

;inserted

Send ("{CTRLDOWN}{HOME}{CTRLUP}")

Sleep (100)

; Adjust Sales Day Book.

Send ("{CTRLDOWN}{PGUP 3}{CTRLUP}")

Sleep (300)

Send("{CTRLDOWN}{END}{CTRLUP}")

Send ("{DOWN}{HOME}")

Send ($date&"/"&$MMYYYY)

Sleep (100)

Send ("{RIGHT}")

$text = InputBox ("GROUP DETAILS", "Enter details", "2 Hours @ Casa Tapas")

Sleep (300)

Send ($text)

Sleep (100)

Send ("{RIGHT 3}")

Send ($count*$price)

Sleep (100)

Send ("{RIGHT 3}")

Send ($count*$price)

Sleep (100)

Send ("{HOME}")

Sleep (100)

Send ("{CTRLDOWN}{PGUP}{CTRLUP}")

$date = InputBox ("GROUP LESSONS", "Enter Date 01 - 31", "")

WEnd

Link to comment
Share on other sites

At the start of the do loop I could possibly have also looped the find student's name section instead of repeating myself. There's lots of sleep time to ensure that certain entries are made correctly before the script continues. I think that I might have tried a While WEnd within Do Until within While WEnd. I don't know why it didn't work. There must be simpler ways of writing this. Skipping sections of script so as not to have to repeat similar actions at different stages. I must upgrade my AutoIt3 to the newest version. This script works fine however, providing you have the correct Excel template which has to be created first.

Edited by czardas
Link to comment
Share on other sites

How do I post a large script? I'm not sure if it will appear correctly.

CODE: AutoIt

Opt("SendKeyDelay", 10)

$Year = InputBox ("START DATE", "Enter the year (4 figures)", "")

$Month = InputBox ("START DATE", "Enter the month 01 - 12", "")

Run("notepad.exe")

WinWaitActive("Untitled - Notepad")

Send ($Month)

.........

......

...

.

My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]
Link to comment
Share on other sites

Here's the file if anyone wants to see it.

My suggestion, for the future, is to re-write this thing from scratch. I can see how it would become unwieldy for you considering you've got a ton of keystroke sends to organize. I can sympathize, believe me.

First and foremost, you should consider using AutoIt's COM capabilities to automate Excel. There is a great ExcelCOM include file written by Randallc which may be of some help (search the forums for "ExcelCOM") - or, if you're willing to wait a little while, I'm in the process of cleaning up one I've written that might be easier to use.

If you're already familiar with using COM in AutoIt, then you're just a skip away.

The problem with sending keystrokes is that, as I'm sure you've noticed, it's too easy to lose control of your script once it gets to automatin'. Click the mouse on a wrong window while it's running and you've got a mess. There's a way to shut off user input during the run, but that's not always a good solution, either.

It's worth investing the time in COM.

-S

(Yet Another) ExcelCOM UDF"A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly...[indent]...specialization is for insects." - R. A. Heinlein[/indent]
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...