Posts: 1,006
Threads: 330
Joined: Mar 2007
Hi All,
I find myself often using "out formatstring" in my functions to track the progress of various variables
Rather than simply having the code say
I use the following
so that I don't get confused about a lot of different variables tumbling out of the Output area
Since I do this so often I wanted to write a function that would automate it for me, like this
Function
OutInt
function str&IntNameToOut #IntValToOut
out "%s = %i" IntNameToOut IntValToOut
so that in calling function, all I would have to write is
The problem is that I don't know how to pass both the Name and the Value of the variable without a second line before the function like this:
str IntNameToOut = "int1"
But typing this is as long as the code I am trying to replace!
Any ideas would be greatly appreciated.
Thanks!!, Stuart
Posts: 11,929
Threads: 138
Joined: Dec 2002
QM does not have a function to get variable name as string. Will need 2 arguments.
OutInt "int1" int1
function $IntNameToOut #IntValToOut
Will not need to type " = %i".
Posts: 1,006
Threads: 330
Joined: Mar 2007
is there a way that the called-function can identify the calling macro/function, get its text as string and then parse the string text for the single argument that contains the variable name (the first block of word text preceded by a space and followed by a space, a semicolon or a new line
I could play with this but I don't know how a macro identifies its calling function's name.
Thanks, Stuart
Posts: 11,929
Threads: 138
Joined: Dec 2002
str s.getmacro(getopt(itemid 1))
But getting qm item text is quite slow.
Posts: 1,006
Threads: 330
Joined: Mar 2007
Hey All,
I got it working for simple case of user not calling lots of user-defined functions with alternating/intercurrent OutVar calls.
Seems to work for the example. Note that must set counting variable to 0 at beginning of a function.
To make it work for more complex case of simultaneous/alternating/intercurrent OutVar calls in multi-threaded usage of QM, I would have to learn about threading which is above me. But see what you think.
I am afraid that for the QM user who would see the need for this (lots of programming), they would probably be using the complex case of multi-threaded user defined variables.
Maybe someone who finds this potentially useful can take this to the next level!
Function
OutVar
function str'IntValToOut ;;receives either str or int and makes into str
int+ OutVarCallCounter ;; need counter because need to know which nth "OutVar" in string is doing the calling
str CallingFunctionText.getmacro(getopt(itemid 1)) ;;gets text of calling function
ARRAY(str) OutVarResults ;; Array for number and nth instance of OutVar
int NumOutVars = findrx(CallingFunctionText "(?<=OutVar\s)\w+(?=(\s|;|\r\n))" 0 4 OutVarResults) ;; gets the var name for each instance of OutVar and places in array
out "%s = %s" OutVarResults[0 OutVarCallCounter] IntValToOut ;; gets the nth var name from OutVarResults array and also the value of that var which is sent with this particular time that OutVar function called
OutVarCallCounter = OutVarCallCounter+1 ;;advances counter
if OutVarCallCounter >= NumOutVars; OutVarCallCounter = 0 ;;resets counter if exceeds total number of OutVar calls
Sample macro to call OutVar multiple times
Macro
out
int+ OutVarCallCounter = 0
int TestInt = 5
OutVar TestInt
TestInt = 7
OutVar TestInt
int hwnd=win("Quick Macros Forum" "IEFrame");err
OutVar hwnd
str HwndProgram.getwinexe(hwnd);err
OutVar HwndProgram
str HwndText.getwintext(hwnd);err
OutVar HwndText
str s = "sample string"
OutVar s
int s_length = s.len
OutVar s_length