AutoHotkey Community

It is currently May 27th, 2012, 11:42 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 28 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: May 7th, 2010, 1:43 am 
Offline

Joined: November 28th, 2009, 4:45 am
Posts: 3089
I found a situation where Goto is the best option. :shock:
The Tray menu on Autohotkey for Pocket PCs has two items that don't work (Windows Spy and Help) and it's missing some things that would help alot. I can't invoke the software keyboard while in the ListLines so I wanted to add shortcuts to the other pages (like KeyHistory). I decided to make a file to include at the top on new scrips that has the new menu in it as well as the labeles in it. I do not want the included file to end the AutoExecute section. The Best way I could think of to have both was to Use Goto to Skip over the labels.
Code:
Menu, Tray, NoStandard
Menu, Tray, Add , Edit, Edit
Menu, Tray, Add , Reload, Reload
Menu, Tray, Add
Menu, Tray, Add , Lines, Lines
Menu, Tray, Add , Vars, Vars
Menu, Tray, Add , Hotkeys, Hots
Menu, Tray, Add , KeyHist, Hist
Menu, Tray, Add
Menu, Tray, Add , Suspend, Sus
Menu, Tray, Add , Pause, Pau
Menu, Tray, Add , Exit, Close
Goto, EndOfNewMenu ;Skip over the labels and continue with autoexecute section
Edit:
Edit
Return
Reload:
Reload
Return
Lines:
ListLines
Return
Vars:
ListVars
Return
Hots:
ListHotkeys
Return
Hist:
KeyHistory
Return
Sus:
Suspend, Toggle
Return
Pau:
Pause, Toggle,1
Return
Close:
ExitApp
EndOfNewMenu:

That way the scripts that use it can still have stuff happen after start up
Code:
#Include, \Storage Card\Autohotkey\NTM.ahk
Test=Wow it Worked
MsgBox %Test%


Last edited by None on May 8th, 2010, 4:46 am, edited 2 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 7th, 2010, 2:37 am 
Offline

Joined: April 8th, 2009, 8:23 pm
Posts: 3036
Location: Rio de Janeiro - RJ - Brasil
Why not GoSub followed by Return?

_________________
"Read the manual. Read it again. Search the forum.
Try something before asking. Show what you've tried.
"
Image
Antonio França
My stuff: Google Profile


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 7th, 2010, 2:52 am 
Offline

Joined: November 28th, 2009, 4:45 am
Posts: 3089
MasterFocus wrote:
Why not GoSub followed by Return?

Because it should not return. If I used Gosub then it would come back to before the labels and I would need a return to end the AutoExecute section so the labels arn't executed.
If I'm Wrong Please post how you would accomplish this.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 7th, 2010, 3:01 am 
Offline

Joined: April 8th, 2009, 8:23 pm
Posts: 3036
Location: Rio de Janeiro - RJ - Brasil
None wrote:
MasterFocus wrote:
Why not GoSub followed by Return?
If I used Gosub then it would come back to before the labels and I would need a return to end the AutoExecute section so the labels arn't executed.

Yes, I know. Once again: why not? I didn't get it.

_________________
"Read the manual. Read it again. Search the forum.
Try something before asking. Show what you've tried.
"
Image
Antonio França
My stuff: Google Profile


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 7th, 2010, 3:28 am 
Offline

Joined: October 26th, 2009, 6:29 am
Posts: 362
Well to my knowledge AHK only "remembers" one previous label.
So....
Code:
Start:
MsgBox,Start
GoSub,Second
MsgBox,Do I show? ;Don't have AHK here so I can't test this code but tis line shouldn't ever be executed to my knowledge
Return

Second:
MsgBox,Second
GoSub,Third
Return ;Come here after showing the third message box, and then return to the GoSub that sent you here

Third:
MsgBox,Third
Return ;Return to under the GoSub that sent you here


Like I said I THINK this is what he is talking about.

Wheres replacing the GoSub,Third with GoTo,Third would allow the message box to show.

_________________
Check out the new AHK forum competition!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 7th, 2010, 3:37 am 
Offline

Joined: April 8th, 2009, 8:23 pm
Posts: 3036
Location: Rio de Janeiro - RJ - Brasil
@Ace Coder: You are incorrect.
This code shows "Start, Second, Third, Do I show?", as expected.

_________________
"Read the manual. Read it again. Search the forum.
Try something before asking. Show what you've tried.
"
Image
Antonio França
My stuff: Google Profile


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 7th, 2010, 4:00 am 
Offline

Joined: November 28th, 2009, 4:45 am
Posts: 3089
Quote:
#Include: Causes the script to behave as though the specified file's contents are present at this exact position.

Simpler Example
Code:
;===Included File====
Hotkey Esc, Lab
GoTo End ;Skip over Label so it won't execute
Lab:
MsgBox The Exit Hotkey worked
ExitApp
Return
End:
;====================
MsgBox I was AutoExecuted
Return
MsgBox I Will never show

If you change the GoTo to GoSub the script automatically exits without pressing Escape.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 7th, 2010, 4:45 am 
Offline

Joined: April 8th, 2009, 8:23 pm
Posts: 3036
Location: Rio de Janeiro - RJ - Brasil
Yes, so you would be forced to use GoTo only if the included file uses GoTo the exact same way. Cyclic logic. Anyway, I can't force you to use GoSub. I hope I never get one of these codes to debug.

_________________
"Read the manual. Read it again. Search the forum.
Try something before asking. Show what you've tried.
"
Image
Antonio França
My stuff: Google Profile


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 7th, 2010, 5:04 am 
Offline

Joined: November 28th, 2009, 4:45 am
Posts: 3089
MasterFocus wrote:
I can't force you to use GoSub.

If you can show me a way to #include "Menu, Tray" commands and their labels without using GoTo I'll Consider it. (Unless you idea is to use two includes one in the Autoexecute section and one after it. :) )


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 7th, 2010, 7:55 am 
Quote:
Situation where Goto is Necessary
Gonorrhea ! :arrow: Goto, aDoctor


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: May 7th, 2010, 3:54 pm 
Offline

Joined: March 10th, 2008, 12:55 am
Posts: 1907
Location: Minnesota, USA
I myself don't mind goto, i do use it every now-and-then, here-and-there.
don't people not like it because it can make code confusing? how? whats so hard to grasp about "okay, if (today is monday the 5th): "go to" the airport and get a 1-way ticket to Jamaica."
on irc, you'll find that most people (noobs, not regulars) find gosub more confusing, why should you Return just to Return (which you do 90% of the time anyways)?

Instead of going back to do nothing, just don't go back and do nothing.

_________________
rawr. be very afraid
*poke*
Note: My name is all lowercase for a reason.
"I think Bigfoot is blurry, that's the problem. It's not the photographer's fault, Bigfoot is blurry. So there's a large, out-of-focus monster roaming the countryside."


Last edited by tidbit on May 7th, 2010, 7:37 pm, edited 2 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 7th, 2010, 5:49 pm 
Offline

Joined: October 7th, 2006, 4:50 pm
Posts: 3157
Location: MN, USA
None wrote:
If you can show me a way to #include "Menu, Tray" commands and their labels without using GoTo I'll Consider it. (Unless you idea is to use two includes one in the Autoexecute section and one after it. :) )
Instead of pulling the new menu and labels into the tray script how about pulling the tray script into new menu and labels, i.e. #include the tray menu script in place of GoTo? That would save a line by eliminating the extra label.

As for the theoretical argument concerning GoTo, there is nothing inherently evil about it; however, it is widely abused and used in places where an alternative would be better. GoTo doesn't create bad programs: bad programmers create bad programs. :twisted: The reason I oppose the use of GoTo (and Gui, Destroy) on these forums is due to the target audience of AHK. This language is intended for non-technical users, the vast majority of whom will never grasp the more powerful and often more readable constructs like loops, blocks, branches, and functions by continually using GoTo as a crutch to support bad program design. For AHK scripters, eliminating GoTo is a great learning exercise, even when it doesn't produce more efficient code.

Finally, since the title of the thread is a situation where GoTo is necessary, I've found it useful to break out of nested loops. Some languages have explicit commands for that, but in AHK GoTo can be simpler than checking a variable in every loop just to break out of the inner-most.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 7th, 2010, 8:34 pm 
Offline

Joined: April 8th, 2009, 8:23 pm
Posts: 3036
Location: Rio de Janeiro - RJ - Brasil
+1 to jaco06046

_________________
"Read the manual. Read it again. Search the forum.
Try something before asking. Show what you've tried.
"
Image
Antonio França
My stuff: Google Profile


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 7th, 2010, 8:40 pm 
Offline

Joined: March 19th, 2006, 5:52 am
Posts: 419
There is no need for GoTo in these examples. And there is less code.
Code:
Hotkey Esc, Lab
MsgBox I was AutoExecuted
Return
MsgBox I Will never show

Lab:
MsgBox The Exit Hotkey worked
ExitApp
Return


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 7th, 2010, 8:47 pm 
It is actually proven, that GoTo is never neccessary:
The structured program theorem proves that the availability of the goto statement is not necessary to write programs; some combination of the three programming constructs of sequence, selection/choice, and repetition/iteration are sufficient to perform any computation.


But GoTo sometimes helps producing shorter code or increase it´s readability. see:http://kerneltrap.org/node/553/2131


Image


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 28 posts ]  Go to page 1, 2  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group