Count of Global node.
How to we count no.of nodes in global.
Comments
set global=$name(^A),count=0
set i="" for { set i=$o(@global@(i)) quit:i="" if $increment(count) }
zw count
Thanks
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
2Otto
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.
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
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
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