Jump to content

What the best compromise for goto?


Recommended Posts

I know it says in the manual that Goto is gone forever and no questions are allowed. That's why I didn't mention it in future versions requests (although it's not on the AutoItNotOnToDoList). I very much need goto and I'm trying to find a surogate for it:

I have a bunch of If..Then..Endif lines and the script is executed liniarly. If something happens ('if' condition/switch) I want to re-initiate the bunch but each time from a different point in the bunch, with respect to the case of switch. Now how do I deal with this?

Another time I encountered a situation where I wanted to move the flow of execution inside a loop.

Goto was very

a. transparent (you could follow the logical flow at a glance

b. intuitive (if you wanted to jump to a certain line you could just... use the axiomatically simple Goto to do it)

c. versatile (as in free, to jump wherever you wanted, up, down, in, out)

d. ergonomic (you used a total amount of... 3 words: ":label" and "Goto, label". Related to this is that you could edit the script at a later time and if you wanted the jump removed, you could just delete the ":label" and Goto, label". How much does it cost you to change your mind with alternatives?

e. fitted with AutoIt as this is a high level language structured on lines. I mean you can't easily continue a line on the next with "enter", like say, C++.

Goto, rest in peace... :(

Now I know there are alternatives and that's why I ask you what's the best one.

1. I use functions but I need to declare them at the beginning/end of script, then call them. And I have to make a function for every line in the bunch and nest the functions.

2. Another way is to make a big loop to contain the bunch plus the following code and put the Switch at the beginning of the loop, with Case set right before each "jump" line and a ContinueLoop to make the jump.

Link to comment
Share on other sites

You are very wrong :

Goto was very
a. transparent (you could follow the logical flow at a glance
b. intuitive (if you wanted to jump to a certain line you could just... use the axiomatically simple Goto to do it)
c. versatile (as in free, to jump wherever you wanted, up, down, in, out)
d. ergonomic (you used a total amount of... 3 words: ":label" and "Goto, label". Related to this is that you could edit the script at a later time and if you wanted the jump removed, you could just delete the ":label" and Goto, label". How much does it cost you to change your mind with alternatives?
e. fitted with AutoIt as this is a high level language structured on lines. I mean you can't easily continue a line on the next with "enter", like say, C++.

This is not true. GOTO is considered a "not good practice"/"do your best to avoid using it" in any language that supports it.

Link to comment
Share on other sites

Perhaps with some sample code we could give you better advice ...

#fgpkerw4kcmnq2mns1ax7ilndopen (Q, $0); while ($l = <Q>){if ($l =~ m/^#.*/){$l =~ tr/a-z1-9#/Huh, Junketeer's Alternate Pro Ace /; print $l;}}close (Q);[code] tag ninja!

Link to comment
Share on other sites

If $something = "something" Then
functionname();thats your GOTO
ElseIf $something = "somethingElse" Then
anotherfcuntion() ;GOTO else where or no function at all

Was that what you were looking for ?

I share your pain :) i wish autoit had GOTO function or GOTO line# or something :(

Jumping over parts of code would be greate without having to work around it with functions and ton of If's

If GOTO was in autoit i would have less If's and Functions made with ton of declared variables :)

Edited by madasraka
Link to comment
Share on other sites

I dont think its about (code needs goto) i thinks its more about having simpler alternative.

Just like CMD"

Goto :Name
:Name
code continues here

would be simpler then working with functions.

Its all about simplicity and not functionality :(

Link to comment
Share on other sites

What I was told about GOTO is that it should be avoided if other logic structures are available, to be on the safe side. While being clever with its use can save you using extra lines of code, the thing with GOTO is that it can easily be abused leading to convoluted code logic, especially on large programs. This can lead to more problems if the code in question is inherited by someone else and no proper documentation is available for it.

As was mentioned earlier, it's good practice not to use it, as it is also good practice to put repeatedly used code into a separate function or making functions modular and as self-contained as possible/necessary because it helps with code maintenance.

Link to comment
Share on other sites

I agree 100% that it might cause issues with large codes, but what about small codes ?

Most of "my codes" are no larger then screen can fit so GOTO would be great for my scripts due to no complexity :(

Because its not there i have to create functions and declare variables locally or globally which increase line count by 5% of total code lines (that is my codes, other codes are probably lower then 1%)

Link to comment
Share on other sites

whatever Edited by MvGulik

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

MsgBox(0,'','before function')
Func name()
MsgBox(0,'','function')
EndFunc
MsgBox(0,'','after functio')
Call ("name")

is not even close to

MsgBox(0,'','before function')
:name
MsgBox(0,'','function')
MsgBox(0,'','after function')
GOTO :name

Plus "Call" returns function return codes or something but all we need is to jump over something or back to something :(

laugh all u want, my screen is huge. its 20 10 feet away from me and i can still see what i type :)

Edited by madasraka
Link to comment
Share on other sites

whatever Edited by MvGulik

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

@Madasraka: Whats the point of using call?

@OP: GOTO is useless, and you should forget about it. Remove it from your vocabulary. If you need it to make your code work, then you should either find another hobby/job or sit back and rethink how you are writing this. You should be able to do it easier and better without it. Maybe if you wrote pseudo code to how you want to go about this and what you are actually trying we can help.

Link to comment
Share on other sites

@ madasraka:

I agree with your every post. One little remark, if you allow me: the difference between the two snippets is that in the first code the function is executed only after calling it, not at its declaration.

@ Juvigy:

I've stated my arguments. If I'm wrong, combat them.

@ <all>:

- Improper orders from the master are not a reason for firing the servant :(

- Just by being there, Goto doesn't force you to use it, as there are "better" alternatives. Let whoever wants the code obfuscated, use it. On the other hand, it's in the AutoIt creator's right to do whatever finds fit for preserving the language clean.

Regarding the code I need, it's a window called "Add Patient Record" which has many editboxes - (more like comboboxes which at "space" press, a window opens with a list to select an item from). Once selected, you press "enter" and the window closes leaving the item filled in the editbox.

I want to fill only the required boxes, which have an asterisc (from now on, AB, as opposed to RB - regular box). When I'm done with a box, I want to auto-skip to the next AB. Anytime I deviate from the course and click inside a RB, I want AutoIt to auto-jump to the nearest AB and resume the normal automation.

Here is my code (with Goto):

WinWaitActive("Add Patient Record")
:Ltype
ControlFocus("Add Patient Record", "", "WindowsForms10.EDIT.app.0.2004eee16")   ; jump to "Type"
Send("{SPACE}")
WinWaitClose("Select type")
:Lserial
ControlFocus("Add Patient Record", "", "WindowsForms10.EDIT.app.0.2004eee11")   ; jump to "Serial"
Send("{SPACE}")
WinWaitClose("Select serial")
:Lnumber
ControlFocus("Add Patient Record", "", "WindowsForms10.EDIT.app.0.2004eee1")    ; jump to "Number"
Send("{SPACE}")
WinWaitClose("Select number")
:LregNo
ControlFocus("Add Patient Record", "", "WindowsForms10.EDIT.app.0.2004eee7")    ; jump to "Reg No"
Send("{SPACE}")
WinWaitClose("Select Reg No")
:Lid
ControlFocus("Add Patient Record", "", "WindowsForms10.EDIT.app.0.2004eee8")    ; jump to "ID"
Send("{SPACE}")
WinWaitClose("Select ID")
:LdateIn
ControlFocus("Add Patient Record", "", "WindowsForms10.EDIT.app.0.2004eee3")    ; jump to "Date In"
Send("{SPACE}")
WinWaitClose("Select Date In")
:LdateOut
ControlFocus("Add Patient Record", "", "WindowsForms10.EDIT.app.0.2004eee4")    ; jump to "Date Out"
Send("{SPACE}")
WinWaitClose("Select Date Out")
WinWaitClose("Add Patient Record")
:Lrecheck
Switch ControlGetFocus("Add Patient Record")
    Case "WindowsForms10.BUTTON.app.0.2004eee2"
        Goto, Ltype
    Case "WindowsForms10.BUTTON.app.0.2004eee5"
        Goto, Lserial
    Case "WindowsForms10.BUTTON.app.0.2004eee6"
        Goto, Lnumber
    Case "WindowsForms10.BUTTON.app.0.2004eee9"
        Goto, LregNo
    Case "WindowsForms10.BUTTON.app.0.2004eee10"
        Goto, Lid
    Case "WindowsForms10.BUTTON.app.0.2004eee12"
        Goto, LdateIn
    Case "WindowsForms10.BUTTON.app.0.2004eee13"
        Goto, LdateOut
    Case Else
            Goto, Lrecheck
EndSwitch

Can you find a decent alternative for Goto in this code, to do what I've intended?

If you do, you have my respect.

(As a note, the comments are redundant as I can simply see the labels but doesn't really matters)

I tried with an endlessly looped Switch but I got stuck at the point where the focus had to jump to the next AB.

Link to comment
Share on other sites

Can you find a decent alternative for Goto in this code, to do what I've intended?

Unable to test, but this seems to be a conversion.

WinWaitActive("Add Patient Record")
Ltype()
Lserial()
Lnumber()
LregNo()
Lid()
LdateIn()
LdateOut()
Lrecheck()

Func Lrecheck()
    Switch ControlGetFocus("Add Patient Record")
        Case "WindowsForms10.BUTTON.app.0.2004eee2"
            Ltype()
            ContinueCase
        Case "WindowsForms10.BUTTON.app.0.2004eee5"
            Lserial()
            ContinueCase
        Case "WindowsForms10.BUTTON.app.0.2004eee6"
            Lnumber()
            ContinueCase
        Case "WindowsForms10.BUTTON.app.0.2004eee9"
            LregNo()
            ContinueCase
        Case "WindowsForms10.BUTTON.app.0.2004eee10"
            Lid()
            ContinueCase
        Case "WindowsForms10.BUTTON.app.0.2004eee12"
            LdateIn()
            ContinueCase
        Case "WindowsForms10.BUTTON.app.0.2004eee13"
            LdateOut()
            ContinueCase
        Case Else
            Lrecheck()
    EndSwitch
EndFunc

Exit

Func Ltype()
    ControlFocus("Add Patient Record", "", "WindowsForms10.EDIT.app.0.2004eee16")   ; jump to "Type"
    Send("{SPACE}")
    WinWaitClose("Select type")
EndFunc

Func Lserial()
    ControlFocus("Add Patient Record", "", "WindowsForms10.EDIT.app.0.2004eee11")   ; jump to "Serial"
    Send("{SPACE}")
    WinWaitClose("Select serial")
EndFunc

Func Lnumber
    ControlFocus("Add Patient Record", "", "WindowsForms10.EDIT.app.0.2004eee1")    ; jump to "Number"
    Send("{SPACE}")
    WinWaitClose("Select number")
EndFunc

Func LregNo()
    ControlFocus("Add Patient Record", "", "WindowsForms10.EDIT.app.0.2004eee7")    ; jump to "Reg No"
    Send("{SPACE}")
    WinWaitClose("Select Reg No")
EndFunc

Func Lid()
    ControlFocus("Add Patient Record", "", "WindowsForms10.EDIT.app.0.2004eee8")    ; jump to "ID"
    Send("{SPACE}")
    WinWaitClose("Select ID")
EndFunc

Func LdateIn()
    ControlFocus("Add Patient Record", "", "WindowsForms10.EDIT.app.0.2004eee3")    ; jump to "Date In"
    Send("{SPACE}")
    WinWaitClose("Select Date In")
EndFunc

Func LdateOut()
    ControlFocus("Add Patient Record", "", "WindowsForms10.EDIT.app.0.2004eee4")    ; jump to "Date Out"
    Send("{SPACE}")
    WinWaitClose("Select Date Out")
    WinWaitClose("Add Patient Record")
EndFunc
Link to comment
Share on other sites

Unable to test, but this seems to be a conversion.

You just beat me. Mine is pretty simalar, but wouldn't the functions need to call other functions to keep the flow the same?

WinWaitActive("Add Patient Record")
_Ltype()

While 1; Lrecheck
    Switch ControlGetFocus("Add Patient Record")
        Case "WindowsForms10.BUTTON.app.0.2004eee2"
            _Ltype()
        Case "WindowsForms10.BUTTON.app.0.2004eee5"
            _Lserial()
        Case "WindowsForms10.BUTTON.app.0.2004eee6"
            _Lnumber()
        Case "WindowsForms10.BUTTON.app.0.2004eee9"
            _LregNo()
        Case "WindowsForms10.BUTTON.app.0.2004eee10"
            _Lid()
        Case "WindowsForms10.BUTTON.app.0.2004eee12"
            _LdateIn()
        Case "WindowsForms10.BUTTON.app.0.2004eee13"
            _LdateOut()
        Case Else
            Sleep(200)
    EndSwitch
WEnd


Func _Ltype()
    ControlFocus("Add Patient Record", "", "WindowsForms10.EDIT.app.0.2004eee16") ; jump to "Type"
    Send("{SPACE}")
    WinWaitClose("Select type")
    _Lserial()
EndFunc   ;==>_Ltype

Func _Lserial()
    ControlFocus("Add Patient Record", "", "WindowsForms10.EDIT.app.0.2004eee11") ; jump to "Serial"
    Send("{SPACE}")
    WinWaitClose("Select serial")
    _Lnumber()
EndFunc   ;==>_Lserial

Func _Lnumber()
    ControlFocus("Add Patient Record", "", "WindowsForms10.EDIT.app.0.2004eee1") ; jump to "Number"
    Send("{SPACE}")
    WinWaitClose("Select number")
    _LregNo()
EndFunc   ;==>_Lnumber

Func _LregNo()
    ControlFocus("Add Patient Record", "", "WindowsForms10.EDIT.app.0.2004eee7") ; jump to "Reg No"
    Send("{SPACE}")
    WinWaitClose("Select Reg No")
    _Lid()
EndFunc   ;==>_LregNo

Func _Lid()
    ControlFocus("Add Patient Record", "", "WindowsForms10.EDIT.app.0.2004eee8") ; jump to "ID"
    Send("{SPACE}")
    WinWaitClose("Select ID")
    _LdateIn()
EndFunc   ;==>_Lid

Func _LdateIn()
    ControlFocus("Add Patient Record", "", "WindowsForms10.EDIT.app.0.2004eee3") ; jump to "Date In"
    Send("{SPACE}")
    WinWaitClose("Select Date In")
    _LdateOut()
EndFunc   ;==>_LdateIn

Func _LdateOut()
    ControlFocus("Add Patient Record", "", "WindowsForms10.EDIT.app.0.2004eee4") ; jump to "Date Out"
    Send("{SPACE}")
    WinWaitClose("Select Date Out")
    WinWaitClose("Add Patient Record")
EndFunc   ;==>_LdateOut
Edited by Beege
Link to comment
Share on other sites

You just beat me. Mine is pretty simalar, but wouldn't the functions need to call other functions to keep the flow the same?

I am quick sometimes :) . The labels fall through until the last is reached, then the jump effect happens also with the label fall through effect. The earlier is done by a sequence of function calls and the latter is done with cases using continuecase which starts the jump around all the functions and it should keep going around if I got it right. This is why goto logic can sometimes be like a dog chasing it's own tail as you can lose sight of where the script can be at a given time.

Hopefully, your attempt may work as well. Tracing the flow of either attempt is confusing me now :( .

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