Passing multiple numbers into a macro

I was asked if it was possible to build a macro with 3 numbers the arguments - so as to generate something like 'Take xx mls yy times a day for zz days'.  It turns out that this is possible using the trick show below (this text has been placed in the CHS text http://www.openvpms.org/documentation/csh/1.7/admin/lookup/macro ):

 

You cannot pass more than one number into the macro, but you can use the following trick. You can use a decimal and then cut it up using an expression like the following:

concat('Part1=',substring-before(string($number),'.'),' Part2=',substring-after(string($number),'.'))

If a macro say @parts is defined with the above expression, then 12.34@parts will expand to 'Part1=12 Part2=34'.

You can even use three parts using the expression:

concat('Part1=',substring-before(string($number),'.'),' Part2=',substring-before(substring-after(string($number),'.'),'.'),' Part3=',substring-after(substring-after(string($number),'.'),'.'))

Now 123.567.789@parts will expand into 'Part1=123 Part2=456 Part3=789'. That is, although we are passing in a number with an illegal format, we can cut it into parts.

The above trick is useful if you a building a macro to generate something like 'Take x mls y times a day for z days'.

If you prefer, you can use / as the separator rather than the decimal point by changing '.' in the above expressions to '/'. You then invoke the macro as 123/456/789@parts.

 

Regards, Tim G

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Re: Passing multiple numbers into a macro

Nice! Perhaps we should think about supporting arbitrary parameters?

E.g.:

@parts(123, 456, Foo)

The parameters would be defined as variables for use in the expression:

 concat('Part1=',$arg0, ', Part2=', $arg1, ', Part3=', $arg2)

The above would give:

Part1=123, Part2=456, Part3=Foo

 

-Tim A

Re: Passing multiple numbers into a macro

Tim A - if on a winter evening, you are wondering what to do for fun, this would be a neat facility to add - it also gets past the limitation of my technique that one cannot pass other than digits.

Regards, Tim G

PS if you do get around to doing it, I would be tempted to use a 1 based arg count rather than a zero based one - it will seem less 'computer speak'.

Syndicate content