Question
Scott Roth · Sep 13, 2018

Operators as Variables

We have the need to write a function that can loop through say a field in an OBX segment within HL7 and compare it to a string passed. Is it possible to have the user enter the Operator ( >,<,=,<>) as a variable inside Cache object script? Does anyone have any examples they can share?

Thanks

Scott Roth

The Ohio State University Wexner Medical Center

00
0 8 309 4

Replies

Are you not using the Transformations?

This is not in a translation. We want to write a function to loop through a repeating segment and compare it to a string value within a Routing Rule. I've done this before as a lookup against a table but not compare strings before.

could you use the function called xecute ?

I am not familiar with that function.

This is how I am looking up a repeatable segment against a lookup table.

ClassMethod GroupIDExists(pHL7Msg As EnsLib.HL7.Message, pSegment As %String, pField As %String, pLookupTable As %String) As %Boolean
{
            #dim tSeg as EnsLib.HL7.Segment
            
            set tSegCount = pHL7Msg.SegCountGet()
            set = 1
            Set tFound = 0
            //get new values
            set tval=""
            while ((<= tSegCount) && (tval="")) {
                         
                        set tSeg = pHL7Msg.GetSegmentAt(i)
                        if (tSeg.Name = pSegment) {
                                    set tID = tSeg.GetValueAt(pField)
                                    set tval= ..Lookup(pLookupTable, tID)
                                                            
                        }
                        set = + 1
            }
            if (tval '= "")
            {
                        1
            }
            quit 0
}

Not sure how you would do that with XECUTE

Here's an example of what Carlos is suggesting with XECUTE:

ClassMethod CompareValues(val1, val2, operator) as %Boolean {
    set testStatement="set testResult=(val1 "_operator_" val2)"
    XECUTE testStatement
    quit testResult
}

[edited code to fix a typo]

Same idea within a shell session. I often like to evaluate expressions to check they work as expected before writing any significant code.

set val1 = 10
set val2 = 11
set operator = "<"
// No need for extra double-quotes in logical expression
set testStatement="set testResult=(val1_operator_val2)"
xecute testStatement
zwrite testResult

I'd try to avoid giving user an ability to enter arbitrary code.

1. Offer user a list of predefined operations ( >,<,=,<> or Equals,More, Not Equals,...)

2. Get input from user.

3. Check that input is actually one option on a list and not something else. 

4. Perform the check using IF, for example.

ClassMethod main(a As %Integer, b As %Integer, operation As %String(VALUELIST="Equals,Not Equals") = "Equals") As %Boolean
{
  set operationList = $lb("Equals", "Not Equals")
  throw:'$lf(operationList, operation) ##class(%Exception.General).%New("<INVALID OPERATION>")
 
  if operation = "Equals" {
    set result = (a = b)
  } elseif operation = "Not Equals" {
    set result = (a '= b)
  } else {
    throw ##class(%Exception.General).%New("<INVALID OPERATION>")
  }
 
  quit result
}

eXecute command is sensible to variable scoping
in addition control of allowed operates is required.
this small method gives you an easy to maintain code.

ClassMethod Operate(var1 = ""op As %String = "",var2 = "") As %Boolean {
 goto $case(op
         ,"<":lt
         ,">":gt
         ,"=":eq
         ,"<>":ne
         ,:fail
         )
fail quit 0
lt   quit var1 < var2
gt   quit var1 > var2
eq   quit var1 = var2
ne   quit (var1 '= var2)
}

.

and you are free to use any naming of your operator you allow ( & , $ , @, GOOFY, DAISY, DUFFY, DONALD, .. )