Question Ponnumani Gurusamy · May 18, 2017

Count of Global node.

How to we count no.of nodes in global.

Comments

Dmitry Maslennikov · May 18, 2017
set global=$name(^A),count=0
set i="" for { set i=$o(@global@(i)) quit:i=""  if $increment(count) }
zw count
0
Otto Medin  May 18, 2017 to Dmitry Maslennikov

That counts the first-level subscripts, but it ignores the root node, as well as deeper-level subscripts, e.g:

USER>set (^x, ^x("a"), ^x("b","c"), ^x("b","d")) = ""
 
USER>set global=$name(^x),count=0
 
USER>set i="" for { set i=$o(@global@(i)) quit:i=""  if $increment(count) }
 
USER>write count
2

Otto

0
John Murray  May 18, 2017 to Otto Medin

In my opinion your method for ensuring the root node gets counted is already elegant, though perhaps worthy of a comment if you're writing it in a routine/method rather than issuing in at the command prompt.

0
Otto Medin  Aug 7, 2017 to Otto Medin

This only matters for very long loops (and you'd need a pretty extreme scenario for it to matter even then), but a post-conditional 'quit' is only meaningful if there's more code after it, and it comes with a performance cost, so this line:

USER>for count = count:1 set ref=$query(@ref) quit:ref=""

...should be:

USER>for count = count:1 set ref=$query(@ref) if (ref="") quit
0
Otto Medin · May 18, 2017
USER>set (^x, ^x("a"), ^x("b","c")) = ""
 
USER>set ref = "^x", count = $data(@ref)#2
 
USER>for count = count:1 set ref=$query(@ref) quit:ref=""
 
USER>write count
3
USER>

Perhaps someone has a more elegant way to make sure the root node gets counted...?

Otto

0
Robert Cemper · Aug 14, 2017

Pls. don't forget to mark your question as "answered" on Developer Community,
please click the checkmark alongside the answer you (as author of the question) accept

0