Hi Mark,

When looping through numerics, unless you intervene, it will loop through every number in magnitude order until you reach the end. So, if you start at 8009 and 8010 exists then it will find it. And 8009 is not next to 800900001 by a long way.

When looping through strings, it's going to go through them like a human would when going through a dictionary. All words with the same prefix are next to each other. So you can easily stop your loop immediately when you find a word that doesn't match.

This code will probably do what you want, athough I haven't tested it:

Noddy ;; Find all subscripts with numeric prefix using minimum $ORDER
    ;
Find(Prefix=8009,MaxSubscriptlength=12)
    Prefix=+Prefix {
        // Must be numeric
        MatchLength=$l(Prefix)
        I=MatchLength:1:MaxSubscriptLength {
            // try starting from every number beginning with
            // 8009, thru 80090, 800900, up to say 800900000000
            // or whatever the maximum subscript length is
            Start=Prefix_$E("000000000000",0,I-MatchLength)
            F  {
                // see if we found one, even at the start of the loop
                $D(^REXREF3(1,Start)) {
                    // but it must have our prefix
                    // this test can end the loop!
                    Q:$E(Start,1,MatchLength)'=Prefix
                    // but only looking for numerics in this loop
                    // don't want to find again later
                    Q:Start'=+Start
                    // Now do whatever it is you want to do with it..
                    // <REMOVE MINE AND INSERT YOUR CODE HERE>
                    !,Start
                }
                Start=$O(^REXREF3(1,Start))
                Q:Start=""
            }
        }
    }
    // Now look for strings with that prefix
    // all strings starting with 8009 will immediately follow "8009 "
    // so start there, there won't be an numeric subscripts following a string
    Start=Prefix
    MatchLength=$l(Prefix)
    Prefix=+Prefix Start=Start_" "
    F  {
        // see if we found one, even at the start of the loop
        $D(^REXREF3(1,Start)) {
            // but it must have our prefix
            // this test can end the loop!
            Q:$E(Start,1,MatchLength)'=Prefix
            // Now do whatever it is you want to do with it..
            // <REMOVE MINE AND INSERT YOUR CODE HERE>
            !,Start
        }
        Start=$O(^REXREF3(1,Start))
        Q:Start=""
    }
    q
 

I had similar problems with the digital signature classes provided with Caché solved by extending the classes and adding the missing properties. Once extended you could add and InclusiveNamespace property and populate it with anything you like. You may find that you have to extend the entire digital signature package or even copy the package to a new one so that you can add what you want.