Jump to content

Can a Select Case with a nested For To Next loop work?


Recommended Posts

Guys,

I'm running in to an issue with one of my scripts working with a nested For To Next loop inside a Select Case.  This isnt the actual script. It's just an example layout.

 

For $i = O To 10 
    Select
        Case $mystring = 1
            ;do this
        Case $mystring = 2
            ;do that
        Case $mystring = 3
             For $iLine = 0 To 10
                For $iCt = 0 To 5
                    msgbox(0,"Counting to Five", $iCt)
                Next    
             Next   
    EndSelect
Next

 

Am I missing something. I can comment out the For To Next loop and everything works as expected. Once I try and run it with the next For To Next loop though, it breaks at that point.

 

 

 

 

Link to comment
Share on other sites

sorry for not being more clear. My select Case loop works perfect with out a "For To Next" nested Loop. When I add a For To Next loop inside as part of the statements as defined in the help file

Select
    Case <expression1>
    statement1
    ...
    [Case <expression2>
    statement2
    ...]
    [Case <expressionn>
    statementn
    ...]
    [Case Else
    statementElse
    ...]
EndSelect

my select case loop breaks and doesn't finish checking the remaining Case <expressions> . Example:

If I have 10 Case <expressions> and matches are made on the first 3 that do not contain a "For To Next" Loop but  the 4th Case does contain a "For To Next" Loop in it's statement, the script skips all the remaining Case <expressions>. Number 4  with the "For To Next" loop did get completed but everything else got skipped that comes after number 4 and basically the script moves on to the next part outside of the Select Case Loop. 

 

Here's my issue. I am renaming files using a Select Case loop. Once the files are renamed I also FTP those files to a server. This is all done in the "statement" after the expression has been matched. This part works perfect. Earlier this week I was told I would need to change some value fields inside those files before I upload them. My thoughts were "no problem". I'll just create a "For To Next" loop inside each one of the statements and change the values that need to be change and then FTP them out all in pretty much one motion inside of statement after the match has been made. 

When I added the For To Next Loop, the script only renamed part of the files like it should have and skipped the remaining ones. When I remove the For To Next loop, the script renames and FTP's like it has been for the past year and a half. 

Edited by RickB75
Link to comment
Share on other sites

Ok... after writing a dummy script and testing since I posted this. I did find out that you can for sure include multiple nested For To Next loops inside a Select Case loop. Sooo.... The most simplest mistake one makes while scripting. Would anyone like to guess what it was????

Link to comment
Share on other sites

Make sure $i is set to the number zero and not the letter o?

Also, I'm curious why people use Select...Case? Since autoit supports swtiching on strings (some languages don't) I think

Switch ($i)
    Case 0
    Case 1
    Case 2
EndSwitch

Looks better and cleaner than

Select
    Case $i = 0
    Case $i = 1
    Case $i = 2
EndSelect

And switch is usually faster. Just doing some random tests and it looks like slightly faster when executing on just a few cases, significantly faster (about 1/2 the time) when executing on a lot of cases (12 cases). Both string and number tests. Also, if...then came in last on all of my tests.

Edited by InunoTaishou
Link to comment
Share on other sites

11 hours ago, RickB75 said:

Sooo.... The most simplest mistake one makes while scripting. Would anyone like to guess what it was????

Probably forgetting to read the helpfile... from "Language Reference / Conditional Statements" :
"With each of these structures (Select & Switch), the first condition that is true controls which group of statements get executed. All subsequent conditions and their associated statements get ignored."

:)

Link to comment
Share on other sites

This was my test script and it worked just as I expected.

#include <Array.au3>
#include <String.au3>

Local $aArrayLoop1[0]

For $i = 1 To 10
    _ArrayAdd($aArrayLoop1,$i)


Next

_ArrayDisplay($aArrayLoop1)

Local $aArray1[10]
Local $aArray2[10]
Local $aArray3[5]

For $i = 0 To UBound($aArray1) -1

        $mystring = $aArrayLoop1[$i]

        Select
            Case $mystring = 1
                MsgBox(0,"found 1 in ArrayLoop1","")
            Case $mystring = 3
                MsgBox(0,"found 3 in ArrayLoop1","Going into For To Loop")
                Sleep(1000)
                For $iCt = 0 To Ubound($aArray2) - 1
                    For $ibox = 0 To UBound($aArray3) - 1
                        MsgBox(0,"Inside 2nd nested For To loop", "Counting Array3: " & $ibox & " | First For To Loop Count is: " & $iCt)
                    Next
                Next
                Sleep(1000)
            Case $mystring = 5
                MsgBox(0,"found 5 in ArrayLoop1","")
            Case $mystring = 4
                MsgBox(0,"found 4 in ArrayLoop1","")
                Sleep(1000)
                For $iCt = 0 To Ubound($aArray2) - 1
                    For $ibox = 0 To UBound($aArray3) - 1
                        MsgBox(0,"Inside 2nd nested For To loop", "Counting Array3: " & $ibox & " | Second For To Loop Count is: " & $iCt)
                    Next
                Next
                Sleep(1000)
            Case $mystring = 2
                MsgBox(0,"found 2 in ArrayLoop1","")
            Case $mystring = 6
                MsgBox(0,"found 6 in ArrayLoop1","")
        EndSelect


Next

In my Main "For To Next" loop, I used $i as my Counter. In my main script I used $i as my main counter and I use it as the counter in my nested "For To Next" loops.

The count for $i was being thrown off for the main loop because of the Nested loop. Once I changed my counter variable from $i to iCt in my nested loops, it worked perfect.  

Link to comment
Share on other sites

Hi @RickB75
Is always sane when you use Nested For Loops use different variables for the loops like for example when you are going to walk trough a 2 dimension array like in this example:
 

Local $iRows = UBound($asResult, $UBOUND_ROWS)
Local $iCols = UBound($asResult, $UBOUND_COLUMNS)

For $i = 1 To $iRows -1
    For $j = 1 To $iCols -1
        $result &= $asResult[$i][$j]&","
    Next
        $result = StringTrimRight($result,1)
        $result &= "|"
Next


Kind Regards
Alien

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...