syntaxparameter-passingintersystems-cacheobjectscript

How do I pass a global or process private global by reference to a procedure


Say I have Caché ObjectScript procedure that expects to receive a by reference array parameter:

TotalArray(Arr)
 S Total=0
 S K=""
 F {
  S K=$O(Arr(K))
  Q:K=""

  S Total=Total+Arr(K)
 }
 Q Total

I can call that procedure with a regular array by reference with the dot syntax:

S A(1)=5
S A(2)=10
W $$TotalArray(.A)

But when I try to do it with a global reference, I get a syntax error with the dot syntax:

S ^A(0)=5
S ^A(1)=10
W $$TotalArray(.^A)

What is the correct way to pass a global array by reference to an ObjectScript procedure? I also want to be able to pass process private globals (the ^||Array convention)


Solution

  • If you use it with indirection as Brandon suggested:

    TotalArray(ArrName)
     S Total=0
     S K=""
     F {
      S K=$O(@ArrName@(K))
      Q:K=""
    
      S Total=Total+@ArrName@(K)
     }
     Q Total
    

    and then you call it like this

    W $$TotalArray("^A")
    

    or even

    W $$TotalArray("^A(""someIndex"")")