AutoHotkey Community

It is currently May 27th, 2012, 12:56 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 13 posts ] 
Author Message
PostPosted: September 17th, 2011, 9:53 am 
Offline

Joined: January 20th, 2007, 7:47 pm
Posts: 110
Is there a way for a function to put into a variable the line number of the line that called the function? Thanks!

_________________
...Ed


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 17th, 2011, 10:12 am 
Offline

Joined: August 7th, 2011, 1:23 pm
Posts: 754
Not so elegant but it works.
Code:
var :="SomeText"
msgbox % TellMeTheLine(var,A_LineNumber)
ExitApp

TellMeTheLine(v,l) {
   return % "You pass the value """ v """ called by line " l
}

Regards


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 17th, 2011, 10:17 am 
Offline

Joined: January 20th, 2007, 7:47 pm
Posts: 110
thanks. i think I understand it. 2 AM here in San Francisco so I'll try it again in teh morning. Thanks again.

_________________
...Ed


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 17th, 2011, 2:04 pm 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7503
Location: Australia
Code:
TheCalledFunction()
TheCalledFunction() {
    MsgBox % Exception("", -1).Line
}

Requires v1.1.04+. See Throw.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 17th, 2011, 2:16 pm 
Offline
User avatar

Joined: December 21st, 2007, 3:14 pm
Posts: 3826
Location: Louisville KY USA
Lexikos wrote:
Code:
TheCalledFunction()
TheCalledFunction() {
    MsgBox % Exception("", -1).Line
}

Requires v1.1.04+. See Throw.
God bless you Lexikos i been passing a_line number for years its a pain in the butt

_________________
No matter what your oppinion Please join this discussion
Formal request to Polyethene
Image


Report this post
Top
 Profile  
Reply with quote  
PostPosted: September 17th, 2011, 7:48 pm 
Offline

Joined: January 20th, 2007, 7:47 pm
Posts: 110
Thanks. Based on what Odlanir wrote, (for which thanks again) I came up with the following to record variable states in a debug file.

Code:
#t::
   Testvariable := "wtf"
   DebugWithThis("TestVariable",A_LineNumber)      
   return
DebugWithThis(VrDbg,LnN)
   {
      LnN -= 1
      DbugLogLn :=  LnN " - " VrDbg "`= |" %VrDbg% "| (" A_Hour ":" A_Min ")"
      fileappend , `n%DbugLogLn%`n , C:\Documents and Settings\Ed\My Documents\AutoHotkey\Use\AutoHotkeyDebugLog.txt
      return
   }

It writes lines like the following into AutoHotkeyDebugLog.txt
Code:
3976 - TestVariable= |wtf| (10:52)
My questions:
    1) Lexikos, thanks, and how would I do the above with your approach instead? This is not urgent: I've solved the problem, but I want to understand what you did.

    [I respect the sparse elegance of the OTB and particularly the "MsgBox % ..." notations. But, I am almost never using the code that way, and I'm often unable to intuitively grasp those examples. Unfortunately for me, those so often are the only kind of examples available, and lack of examples I can experiment with is most often what leads me to I post a question here. In any case, if you're willing, please use variables as you explain. Thanks]

    I did look at Throw, but I don't understand the examples in the help. Right dominant brain trying to play a left-brain game, I guess.

    2) I have assigned the full address of my debug log to a variable upon starting my script. I can read the file that way elsewhere in the script, but the variable doesn't seem to deliver the address to my function; why?
Thanks!

_________________
...Ed


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 17th, 2011, 10:23 pm 
Offline
User avatar

Joined: September 8th, 2008, 12:26 am
Posts: 1048
Location: Ploieşti, RO
See definition of functions and usage of variables inside functions. In your particular case, if you want the variable defined outside the function to be recognized inside the function, you'll have to define it as Global in the beginning of the function, unless you want to pass it as yet another parameter (not recommended since it may be a long string):
Code:
DbgLogPath := A_MyDocuments "\AutoHotkey\Use\AutoHotkeyDebugLog.txt"

DebugWithThis(VrDbg,LnN)
   {
    Global DbgLogPath ; prevously defined variable
[...]
    }
I hear you on the brain thing... happens here all the time. :)

_________________
AHK tools by Drugwash (AHK 1.0.48.05 and Win98SE)


Report this post
Top
 Profile  
Reply with quote  
PostPosted: September 18th, 2011, 1:15 am 
Offline

Joined: January 20th, 2007, 7:47 pm
Posts: 110
Ah, define it as Global, gotcha! Thanks!

I now have re-read docs on functions and variables and things are becoming a bit more clear. I also read around the forum: it sounds as if <gasp> global variables are satanic devices! Well _I_ can stand up to anything...

<sigh> except temptation: Can I declare my AutoHotkeyDebugLog variable to be global when I first load it (upon starting the script), and thus, know that that never-changing value will always be available for any function?

Or is there some kind of shorthand other than a variable, that I could use for often-reused items like a file name and path? THANKS

...Ed

_________________
...Ed


Report this post
Top
 Profile  
Reply with quote  
PostPosted: September 18th, 2011, 1:57 am 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7503
Location: Australia
EdScriptNewbie wrote:
I want to understand what you did.
I'll narrow down the documentation for you:
Quote:
Exception(Message [, What, Extra])

Creates an Exception object.

[The What parameter] can be a string or a negative offset from the top of the call stack. For example, a value of -1 sets Exception.What to the current function or subroutine and Exception.Line to the line which called it.


Source: Throw
Code:
TheCalledFunction()
TheCalledFunction() {
    exception_object := Exception("", -1)
    line_number := exception_object.Line
    MsgBox %line_number%
}
An Exception object is created which contains the filename and line number of the line that called TheCalledFunction. The Line field of this object is then retrieved and shown in the MsgBox. % (followed by space) was used in the previous example to indicate that what follows is an expression; this is often necessary because objects can't be used effectively with the %traditional% syntax.

You can just use the expression from my previous example (Exception("", -1).Line) directly in your function:
Code:
LnN := Exception("", -1).Line - 1  ; And remove LnN from the parameter list.

Quote:
Can I declare my AutoHotkeyDebugLog variable to be global when I first load it (upon starting the script), and thus, know that that never-changing value will always be available for any function?
Not in v1. It mostly works in the current v2 alpha:
Code:
Func0() {
    MsgBox %AutoHotkeyDebugLog% :(  ; Current limitation.
}

global AutoHotkeyDebugLog := A_ScriptDir "\Debug.Log"
; Subsequent references in functions need not be declared.

Func0()
Func1()

Func1() {
    MsgBox %AutoHotkeyDebugLog%
}

Quote:
Or is there some kind of shorthand other than a variable, that I could use for often-reused items like a file name and path?
You can use a function.
Code:
Func1()

Func1() {
    MsgBox % V("AutoHotkeyDebugLog")
}

V(n) {
    static AutoHotkeyDebugLog := A_ScriptDir "\Debug.Log"
    ; Any number of other variables may be added here.
    return (%n%)
}
This also works if AutoHotkeyDebugLog is a global variable, since dynamic variable references (a.k.a. double-derefs) don't need global declarations in v1 (but since it's a common cause of confusion, they do in v2).


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 18th, 2011, 2:02 am 
Offline
User avatar

Joined: September 8th, 2008, 12:26 am
Posts: 1048
Location: Ploieşti, RO
Global declarations (as you probably mean it) are valid only inside functions.
Any variables declared/initialized in the autoexec section (at the beginning of a script) are always available throughout the script, but accessible inside functions only when declared as Global inside the respective functions.

So your debug log path will be available to menus, hotkeys, subroutines (labels), etc. but functions will only see it if declared as Global.

Strings can be passed by handle too, but only where there is code that can manipulate that handle (DllCall() or other newer function/command in AHK_L that I'm not familiar with); FileAppend cannot, AFAIK, so it would be useless to pass it a handle to the log. Either way, that handle would have to be declared as Global too, or passed as a function parameter.

EDIT: Lexikos beat me to it... again. :)

_________________
AHK tools by Drugwash (AHK 1.0.48.05 and Win98SE)


Report this post
Top
 Profile  
Reply with quote  
 Post subject: THANKS!!!
PostPosted: September 18th, 2011, 5:21 am 
Offline

Joined: January 20th, 2007, 7:47 pm
Posts: 110
Thank you both very much. I've learned a lot from my first couple of read-through's, and will get more I'm sure.

Lexikos, I saw the link to v2 alpha, and, <poof!> there went another 45 minutes, spent in awe of the work of art that is AutoHotkey. I can't say I understood all of what I read there. Imho, your writing is crystal clear, so I picked up a lot from context. I can't even begin to imagine the amount of work in this project.

So now, this function thread seems more important to me than I realized: it looks to me as if V2 increases the emphasis on functions. I've been locked in the land of commands and expressions; I guess they fit my learning style better than functions and objects.

My style is to find examples, infer what I can by inspection, bash on them to sorta fit what I want, tinker & test til they work. I'm finding it much harder to learn functions, objects and so on, partly because I'm not finding examples that I can clone, so it's hard to use my pattern brain to figure this stuff out. I want to learn. Do you have any suggestions, where I could find more examples?

I guess this throw-catch-try-exception stuff is brand new, yes? I haven't seen it before. If you could point me to a few more examples of using it, I'd be grateful. If not, no worries, I can see how busy you guys are.

Thanks for your help, and for all you're doing for AHk!

_________________
...Ed


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 18th, 2011, 11:05 am 
Offline

Joined: October 13th, 2009, 10:09 pm
Posts: 1389
Exception and try-catch-trow syntax was introduced in the latest AHK_L release. Functions basically work like this:
Code:
SomeVariable := 8
returnValue := Function("test", 5, SomeVariable) ;Calling a function and assigning its return value.
Msgbox % returnValue ;Using a single % with a space treats everything after it as expression
MsgBox % SomeVariable ;The function could modify the value of SomeVariable since it was passed by reference.

Function(Param1, Param2, ByRef Param3) ;Definition of the function with its parameters
{
    Param3 := 4 ;Change the value of the third parameter which is passed by reference (i.e. a reference to the SomeVariable variable in this case
    return Param1 Param2 ;Return a concatenation of the first two parameters
}


Report this post
Top
 Profile  
Reply with quote  
PostPosted: September 19th, 2011, 6:16 am 
Offline

Joined: January 20th, 2007, 7:47 pm
Posts: 110
Ah, "ByRef Param3" means that SomeVariable got to keep its new
value, 4. I took out ByRef and got 8. yeah! Thanks for sticking with me;
my "frustrated<--/>fascinated" meter now is way over to the
peg on the right. Thx again, y'all. ...Ed

_________________
...Ed


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], BrandonHotkey, chaosad, Google Feedfetcher, MSN [Bot], Yahoo [Bot] and 21 guests


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