Jump to content

Helping ending a function from within an If...Then statement


Recommended Posts

I'm having a problem figuring out how to end Func RqstChk () and return to a previous function (Func GetRqst) or move on to the next function (Func Action).

What is happening is:

1) Func Initiate () is started which calls Func GetRqst ()

2) Func GetRqst () is started & completed

3) Func Initiate () is continued which now calls Func RqstChk ()

4) Fun RqstChk () is started

4a) C:\log.txt is read (it does not contain a string "You told...." for $4b below)

4b) A string between "You told " and ", 'What would you like?'" is not found (due to #4a above)

4c) The array returns an empty value for $Who

4d) If Not IsArray($Who) Then is satisfied

4e) Call ("GetRqst") is performed

5) Func GetRqst () is started & completed

6) The program now returns to where it left off in Func RqstChk () and finds the "Endif" for the "If Not IsArray($Who) Then" statement

7) $Rqst = _StringBetween($File, $Who[0] & " told you, '","'.") is now attempted

7a) Since $Who is an empty array (from #4c-#4d above) #7 can not be completed and an error ("Subscript used with non-Array variable") is returned.

So it seems to me that I need to end/exit the Func RqstChk () from within the "If Not IsArray($Who) Then" statement.

Func Initiate ()
    <some code>
    Call ("GetRqst")
    Call ("RqstChk")
EndFunc

Func GetRqst ()
    <Some code>
EndFunc

Func RqstChk () 
    $File = FileRead("C:\log.txt")
    $Who = _StringBetween($File, "You told ",", 'What would you like?'")
    If Not IsArray($Who) Then;happens when log.txt does not contain a string value for $Who
        Call ("GetRqst");want to end func RqstChk here and go back to Func GetRqst
    EndIf
    $Rqst = _StringBetween($File, $Who[0] & " told you, '","'.")
    If Not IsArray($Rqst) Then;happens when log.txt does not contain a string value for $Rqst
        Call ("GetRqst");want to end func RqstChk here and go back to Func GetRqst
    EndIf
    Switch $Rqst[0]
        Case "Rqst1", "Rqst2", "Rqst3", "Rqst4", "Rqst5", "Rqst6", "Rqst7", "Rqst8", "Rqst9", "Rqst10", "Rqst11", "Rqst12", "Rqst13", "Rqst14", "Rqst15"
            Call ("Action",$Rqst[0]);want to end func RqstChk here and move on to Func Action passing it $Rqst[0]
        Case Else
            Call ("GetRqst");want to end func RqstChk here and go back to Func GetRqst
    EndSwitch
EndFunc 

Func Action ($Rqst)
    <Some code>
EndFunc

absolution (Tault)

Edited by crazyjts
Link to comment
Share on other sites

Thanks Dantay

Try using the "Return" keyword. If you just want to end the function, then you can replace the call function in the if statement with "Return".

I partially understand. Which technique below will end the Func RqstChk () and bring me back to Func GetRqst ()

If Not IsArray($Who) Then

Return ("GetRqst")

EndIf

or

If Not IsArray($Who) Then

Return ("RqstChk")

EndIf

or

If Not IsArray($Who) Then

Return

EndIf

Link to comment
Share on other sites

You can't return to some function from out of the tree you came this function to.

When you return, no matter what data you'll return it'll jump back to the function called it so return("Blah") does not return to "Blah" if "Yada" called it :P but it returns the string "Blah" and end the execution of the function even though there are few more statements to execute.

Link to comment
Share on other sites

Thanks Dantay

I partially understand. Which technique below will end the Func RqstChk () and bring me back to Func GetRqst ()

If Not IsArray($Who) Then

Return ("GetRqst")

EndIf

or

If Not IsArray($Who) Then

Return ("RqstChk")

EndIf

or

If Not IsArray($Who) Then

Return

EndIf

If I understand you correctly then I think what you're trying to do is quite ok and the way you are trying to do it is a sensible attempt but it just isn't quite right. There is no need to use Call IMO. Call is useful when you don't know the function name when you write the script because you can use a string for the finction name but otherwise it is best not to use it.

This is one way you could achieve the aim of "going back" to a function. I'm not sure if you still need to repeat the GetRqst after calling Action.

Func Initiate()
     < some code >
    Do
        GetRqst()
    Until RqstChk() <> -1
EndFunc  ;==>Initiate

Func GetRqst()
     < Some code >
EndFunc  ;==>GetRqst

Func RqstChk()
    $File = FileRead("C:\log.txt")
    $Who = _StringBetween($File, "You told ", ", 'What would you like?'")
    If Not IsArray($Who) Then;happens when log.txt does not contain a string value for $Who
        Call("GetRqst");want to end func RqstChk here and go back to Func GetRqst
    EndIf
    $Rqst = _StringBetween($File, $Who[0] & " told you, '", "'.")
    If Not IsArray($Rqst) Then;happens when log.txt does not contain a string value for $Rqst
        Return -1;Call ("GetRqst");want to end func RqstChk here and go back to Func GetRqst
    EndIf
    Switch $Rqst[0]
        Case "Rqst1", "Rqst2", "Rqst3", "Rqst4", "Rqst5", "Rqst6", "Rqst7", "Rqst8", "Rqst9", "Rqst10", "Rqst11", "Rqst12", "Rqst13", "Rqst14", "Rqst15"
            Return Action($Rqst[0]);want to end func RqstChk here and move on to Func Action passing it $Rqst[0]
        Case Else
            Return -1;Call ("GetRqst");want to end func RqstChk here and go back to Func GetRqst
    EndSwitch
EndFunc  ;==>RqstChk

Func Action($Rqst)
     < Some code >
EndFunc  ;==>Action
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
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...