Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Having Trouble Setting up Simple User-Defined Function
#1
Hi,

I am trying to simplify my scripts by putting an often repeated step in a function

say
Code:
Copy      Help
GetDemographics

When GetDemographics is run in the main macro script (Let's call it "mainscript"), it should call up the code in GetDemographics which will find out values for str variables a, b, c, d and e by running the script in GetDemographics.
I would like these values to be passed back to the caller script ("mainscript").

For simplicity's sake let just say

that GetDemographics code would be:



Code:
Copy      Help
a=apple
b=banana
c=canary
d=dog
e=elephant


Now, after GetDemographics is completed running, mainscript should have values for a, b, c, d, e as determined above in GetDemographics.

It seems like I should be able to do this easily but I am getting confused with variable scope (str+) i.e. do I have to define the variables locally in both caller and GetDemographcis? Do I need to define receiver variables in first line of function? Just can't seem to get it to work.

Thanks for any help....

Stuart
#2
macro:
str a b c d e
GetDemographics a b c d e

---

function GetDemographics:
function str&a str&b str&c str&d str&e
a=apple
b=banana
c=canary
d=dog
e=elephant
#3
Thanks a million, Ginataras! This is the base that will allow me to understand and utilize the rest of the great tips in the help document and forum!
Stuart
#4
stupomer Wrote:...great tips in the help document...

there's a help document?
:wink:
An old blog on QM coding and automation.

The Macro Hook
#5
Since I don't come from a programming background, some of the explanations in the help file are hard for me to understand at first, but once I get the general hang of things, I then can understand the more advanced functionality described in the help pages.
Maybe the community can write more "step-by-step" explanations as a primer for beginners like me (I geuss I am an advanced beginner now!), like you are already doing with your blog. Great job supplementing an already fantastic program..

Stuart
#6
thanks.

i make that 'help file' comment in jest of course as i see so much posted in the past that's pretty obviously not been looked up in the help. Smile but then again, maybe that's just a matter of skulking around here for so long... :wink:
An old blog on QM coding and automation.

The Macro Hook
#7
Can Function work to run new screen interactions to get it's information
i.e.
There is a series of step that I need to run over and over again in many different macros - not just a series of operations on a string, for example.

Is it better to use a function for this or rather just to launch a macro from within the macro. If I do it this way, then I need to use global variables to share the results, right? How can I get the receiving macro to wait for the returned value if I do this?
Or is functions the way to go on this type of thing?
Thanks,
Stuart
#8
Hi,
I think I can just call the macro that has the automated steps from within the second macro and then just have

Code:
Copy      Help
str+ a b c d e

at the beginning of both macros.

I got it working by just putting a wait statement of 2 seconds while the second macro is running through its steps and filling the str variables. However, since this could be variable in time, is there a way to have the main macro wait until these variables change or are updated, etc? I saw there is a Wait for Variable choice in the Wait menus but this seems to be only for integer type variables.

Thanks!,
Stuart
#9
Functions can execute the same code as macros. You may need to insert spe -1 at the beginning to set the same speed as for macros.

If macro1 needs to execute macro2 and wait until macro2 returns, usually it is better if macro2 is function, and macro1 calls macro2 as function, like in this example:

macro2

or

macro2 argument1 argument2 ...

or

somevariable=macro2(argument1 argument2 ...)

When macro1 needs to launch macro2 and not wait for it, use mac:

mac "macro2"

At least one of them should be function, because two macros cannot run simultaneously. If both are macros, macro2 starts when macro1 ends.

If macro1 needs to launch macro2 in separate thread, it can use mac and wait. It is used rarely. Since mac returns thread handle, macro1 can wait for it:

wait 0 H mac("macro2")

Or can wait for some other event, eg global int variable (declared like int+ varname). Cannot wait for str variables, but when you modify a str variable, you can set an int variable to 1.

Using global variables to exchange values between macros is easy, but you should use unique variable names, for example g_whatitdoes, but not a or b.
#10
Still having trouble setting this up

So Macro 1 says:

Code:
Copy      Help
str+ mrn exam[
GetDemographics mrn exam;; this is the function name
outp "The patient medical record number is %s and the exam code is%s" mrn exam

Macro 2 is the function "GetDemographics" as follows:

Code:
Copy      Help
function str&mrn str&exam
spe -1

;; some steps that fill the clipboard
mrn.getclip
;;some additional steps that fill the clipboard with different text
exam.getclip
end


QM is saying invalid pointer for mrn. I am not sure what this means or how to change this? Anything to do with the operator "*" ?

Thanks!,
Stuart
#11
You probably launch the function using the Run button. To prevent accidental launching, insert line containing space and / at the beginning of the function.

Don't use end, unless you don't want to continue macro1.

Don't use + with str, unless you need to share the variable directly, not through arguments.
#12
I got rid of "end" and made str+ into str and everything works great. I geuss that is why we pass arguments instead of global variables!!

Know that I got the hang of it, I can go through and clean out all my repetitive code.
Thanks so much!
Stuart


Forum Jump:


Users browsing this thread: 1 Guest(s)