Question
· May 12, 2023

HOW TO SEARCH IF A STRING EXISTS IN A LOOKUP TABLE

Hello,

 

I am quite new to the InterSystems world. I have one of my OBX 5 fields having this value "blood work was done<>tested positive for anemia<>tested negative for hepatitis". I have a lookup table "AnemiaResults" with a key of TESTED POSITIVE FOR ANEMIA. My issue is that since OBX 5 has other characters/text  (and OBX is a repeating segment) , I am unable to use the Exists function. Any idea how to tackle this? Any assistance will be greatly appreciated. 

Product version: IRIS 2021.2
Discussion (8)2
Log in or sign up to continue

Hi @Christine Nyamu 

Use $Piece function to get the value of a delimited string. In you specific case:

Set value = $Piece(obxValue, "<>", 2)

I assumed that the variable obxValue was set before in the code.

Note if the in the obx segment the value change the order like, "TESTED POSITIVE FOR ANEMIA<>BLOOD WORK WAS DONE<>TESTED NEGATIVE FOR HEPATITIS", the above sample code will fail

Hi Christine,

Please consider if the following snippet may be helpful.

Includes some common checks to prevent invalid lookup

Class Test.Fun Extends Ens.Rule.FunctionSet
{

ClassMethod ExistsAny(pTable As %String = "", pValue As %String = "", pDelimiter As %String = "<>") As %Boolean [ Final ]
{
  Quit:""=pTable 0
  Quit:""=pValue 0
  Quit:""=pDelimiter 0
  Set result=0
  // Use Length Function to count delimited fields. Always one or more
  Set len=$Length(pValue,pDelimiter)
  For i=1:1:len {
    Set key=$Piece(pValue,pDelimiter,i)
    // strip white space at front and end
    Set key=$ZSTRIP(key,"<>W")
    // Avoid empty string for key lookup
    Continue:""=key
    // make uppercase for case insensitive search
    Set key=$ZCVT(key,"U")
    If $Data(^Ens.LookupTable(pTable,key)) {
      Set result=1
      Quit
    }
  }
  Return result
}
}