Gabriel Santos · Jan 9

How to know the size of a global

Exist some command for return the quantity row of a global?



Total rows = 4

Product version: Caché 2018.1
$ZV: 5.659.0
0 214
Discussion (10)4
Log in or sign up to continue

Set count=0,row="" For  { Set row=$Order(^test(row)) Quit:row=""  Set count=count+1 }

Are you looking to count nodes that have data?   Will your global always be sequencially and numerically subscripted like your example?

The order of keys will be random and count keys with no data:


The command will return 3

If you have multiple subscript levels, this may help:

SET I=0,G="^test("""")" FOR {SET G=$QUERY(@G) Q:G=""  SET I=I+1 WRITE G,!} WRITE "Total: ",I,!

Here's the data:

set ^test(1)="aa"
set ^test(1,1,1)="aa"
set ^test(2)="aa"
set ^test(2,1)="aa"
set ^test(2,2,1)="aa"
set ^test(3,1)="aa"
set ^test(4,1)="aa"

And here's the output:

Total: 7

If you only wanted the total (especially if the global is much larger) omit the 'WRITE G,!' portion out of the line of code above.

Hope this helps!


Don't know if it's your case, but if you are able to generate the global data, you could use the $INCREMENT() function, which automatically stores the array length into global's head:

Set ^test($INCREMENT(^test)) = "aa"

Set ^test($INCREMENT(^test)) = "aa"

Set ^test($INCREMENT(^test)) = "aa"

Set ^test($INCREMENT(^test)) = "aa"

ZWrite ^test

Write ^test



Your answer is interessant, but what i do for kill datas:

Set ^test($INCREMENT(^test)) = "aa"
Set ^test($INCREMENT(^test)) = "aa"

kill ^test(2)

ZWrite ^test

Exist some command for decrement?

Sure, the $Increment command can also decrement, and the values don't have to be 'just 1.'

Here's the documentation for the $Increment command:

Simply, you can do this to decrement by 1:

Set TEMP=$INCREMENT(^test,-1)

If you wanted to increment by 10, the command would be this:

Set TEMP=$INCREMENT(^test,10)

To test, try this:

S ^test=0 F  S TEMP=$INCREMENT(^test,10) Q:$G(^test)>100  W ^test,!

Hope this helps!

Why do you want to know this, Gabriel? For some application logic or to estimate size of the global? Or for something else?

Out of curiosity, is there a way to do it with 'for', but i wanted to know a method for that.