Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sending Variable's Name and Value to a function
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

Copy      Help
out int1
I use the following
Copy      Help
out "int1 = %i" int1

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
Copy      Help
function str&IntNameToOut #IntValToOut

out "%s = %i" IntNameToOut IntValToOut

so that in calling function, all I would have to write is
Copy      Help
OutInt int1

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:

Copy      Help
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
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".
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
str s.getmacro(getopt(itemid 1))

But getting qm item text is quite slow.
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
Copy      Help
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

Copy      Help
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

Forum Jump:

Users browsing this thread: 1 Guest(s)