Search code examples
objectscript

Can $bit() create variables here?


Code extracted from this project:

Method RefineSearch(pNewCriteria As %String) As %Status [ ZenMethod ]
{
    set tSC = $$$OK
    try {
        set ..criteria = ..criteria _$s(..criteria="":"",1:",")_pNewCriteria

        set tType = $piece(pNewCriteria,":",1)
        if tType="prop" {
            set tSQL = "SELECT ID FROM "_..tableName_" WHERE "_$piece(pNewCriteria,":",2)_" = '"_$piece(pNewCriteria,":",3)_"' AND %ID %FIND Demo_SearchPortal.Find("_..qid_")"
            set tResult = ##class(%SQL.Statement).%ExecDirect(,tSQL)
            while tResult.%Next() {
                set tID = tResult.%GetData(1)
                $$$IFBITOFFPOS(tID,tChunk,tPos)
                set $bit(tBits(tChunk),tPos)=1 // <-- HERE
            }
            // compress bitmaps
            set tMaxChunk=$order(tBits(""),-1)
            for tChunk = 1:1:tMaxChunk {
                set tBits(tChunk) = $bitlogic(tBits(tChunk))
            }
        } else {
            set tFinder = $classmethod(..className, ..indexName_"Embedded")
            if (tType = "entity") {
                do tFinder.PrepareFind("{"_$piece(pNewCriteria,":",2)_"}", ..searchMode)
            } elseif (tType = "word") {
                do tFinder.PrepareFind($piece(pNewCriteria,":",2), ..searchMode)
            }
            merge tOldBits = ^CacheTemp.IF.SearchPortal.Query(..qid,"b")
            set tChunk=""
            for {
                set tBits = tFinder.NextChunk(.tChunk)
                quit:tChunk=""
                set tBits(tChunk)=$bitlogic(tBits & tOldBits(tChunk))
            }
        }

        set tNewQID = $i(^CacheTemp.IF.SearchPortal.Query)
        merge ^CacheTemp.IF.SearchPortal.Query(tNewQID,"b") = tBits
        set ^CacheTemp.IF.SearchPortal.Query(tNewQID, "parent") = ..qid
        set ..qid = tNewQID

    } catch (ex) {
        set tSC = ex.AsStatus()
    }
    quit tSC
}

At the place marked <-- HERE, this is the first occurrence of tBits -- with a subscripted access to boot.

Is this really legal?


Solution

  • Yes $bit can be used as left side function in SET command, and in this case any legal variable, legal as well. And if this variable was undefined will get new value. As well as $bit also available $piece and $extract, you can see it in documentation