Marek Bernád · Jul 3, 2017

How to delete Caché global by its value in Caché ObjectScript?

Good day, I have this issue. I have global like: ^myglobal(varX) = $LB(var1,var2). I need to delete all globals that have variable var1 in that list same without a need to iterate through all globals and checking their value.

Exact example:

- in database exist following:

^myglobal("Marek") = $LB("code2",1400)
^myglobal("Ondrej") = $LB("code1",12)
^myglobal("Nikita") = $LB("code2",32)
^myglobal("Evgeny") = $LB("code1",76)
^myglobal("Maks") = $LB("code2",99)
^myglobal("Eduard") = $LB("code1",14)

Now! How to delete all globals that have in list in the first place value "code2" please? It doesn't have to be LIST, it can be any other simple value. 

But this question can be put in other way like this:

How to prevent to not set new globals with same value? In other words, when I am creating new global I want to guarantee that its value is UNIQUE in order to other globals.

Thank you in advance for your answers.

0 3 625 1


set key = ""
set var1 = "code2"
for  {
    set key = $order(^myglobal(key), 1, list)
    if $listget(list, 1) = var2 kill ^myglobal(key)

Use $order to iterate over global subscripts.

ZKILL will only delete the node, not the subtree

If you are working directly with globals (as your example seems to imply) you will have to iterate appropriately through your global, test the relevant value, and kill any node that matches.

Be sure that you also understand that a KILL will delete any subtree of the node you kill.

To efficiently maintain the sort of uniqueness you seem to be describing (i.e. for any Y there must be no more than one X for which $list(^myglobal(X))=Y) you will probably need to maintain a cross-reference (a.k.a. index) such as ^myglobalX("index1",Y)=X and check for the existence of a record here before saving a new record.