User bio
404 bio not found
Member since Aug 20, 2020
Posts:
Alan has not published any posts yet.
Replies:

Class Test.Test1 Extends (%RegisteredObject, %XML.Adaptor)
{

ClassMethod RecursiveGlobalCount(pGlobalName As %String, pKey As %String, pCount As %Integer) As %Integer
  {
      /// pCount is zero if not provided
      Set pCount = +$Get(pCount)
      Set tKey = $Get(pKey)

      //pKey should only be undefined on first run
      Set tKey = $Query(@pGlobalName@(tKey))
      If (tKey '= "") Set pCount = 1 + pCount // setting tKey got the first node

      For {
          Set tKey = $Query(@tKey)
          If ((tKey = "") || (tKey '[ pKey)) Quit
          Set pCount = 1 + pCount
      }
      Quit pCount
  }

}

Testing:

    Write  ##class(Test.Test1).RecursiveGlobalCount("^Locations","",0)

    5

    Write  ##class(Test.Test1).RecursiveGlobalCount("^Locations","Canada",0)

    2

    Write ##class(Test.Test1).RecursiveGlobalCount("^Locations","USA",0)
    3

To specify the list of public variables for the method, use the following syntax:

Method name(formal_spec) As returnclass [ PublicList = variablelist ]
{
   //implementation
}

Where publiclist is either a single variable name or a comma-separated list of variable names, enclosed in parentheses.

In the documentation search for publiclist and you can get the details

$Query is the command that will traverse the Global.  Below is my version of the task at hand with testing.  

Class Test.Test1 Extends (%RegisteredObject, %XML.Adaptor)
    {

ClassMethod RecursiveGlobalCount(pGlobalName As %String, pKey As %String, pCount As %Integer) As %Integer
    {
        /// pCount is zero if not provided
       Set pCount = +$Get(pCount)
       Set tKey = $Get(pKey)

       //pKey should only be undefined on first run
       Set tKey = $Query(@pGlobalName@(tKey))
        If (tKey '= "") Set pCount = 1 + pCount // setting tKey got the first node

        For {
            Set tKey = $Query(@tKey)
            If ((tKey = "") || (tKey '[ pKey)) Quit
           Set pCount = 1 + pCount
       }
      Quit pCount
}

}

Testing:

    Write  ##class(Test.Test1).RecursiveGlobalCount("^Locations","",0)

    5

    Write  ##class(Test.Test1).RecursiveGlobalCount("^Locations","Canada",0)

    2

    Write ##class(Test.Test1).RecursiveGlobalCount("^Locations","USA",0)
    3
 

Certifications & Credly badges:
Alan has no Certifications & Credly badges yet.
Global Masters badges:
Followers:
Alan has no followers yet.
Following:
Alan has not followed anybody yet.