Jump to content
Sign in to follow this  
crazyjts

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

Recommended Posts

crazyjts

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

Share this post


Link to post
Share on other sites
dantay9

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


[font="Verdana"] [size="2"]"[/size][/font]Failure is not an option -- it comes packaged with Windows"[font="Verdana"][size="2"] Gecko Web Browser[/size][/font][font="Verdana"][size="2"], [/size][/font][font="Verdana"][size="2"]Yahtzee![/size][/font][font="Verdana"][size="2"], Toolbar Launcher (like RocketDock)[/size][/font][font="Verdana"][size="2"]Internet Blocker, Simple Calculator, Local Weather, Easy GDI+ GUI [/size][/font][font="Verdana"][size="2"]Triangle Solver, TCP File Transfer, [/size][/font][font="Verdana"][size="2"]Valuater's Autoit Wrappers[/size][/font][font="Verdana"][size="3"][size="2"][size="2"]OOP In AutoIt[/size][/size][/size][/font][font="Verdana"][size="2"][size="1"]Using Windows XP SP3, 1GB RAM, AMD Athlon Processor @ 2.1 GHzCheck me out at gadgets.freehostrocket.com[/size][/size][/font]

Share this post


Link to post
Share on other sites
crazyjts

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

Share this post


Link to post
Share on other sites
Authenticity

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.

Share this post


Link to post
Share on other sites
martin

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.

Share this post


Link to post
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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.