Written by

Developer
Question Gabriel Santos · Jan 9, 2023

How to know the size of a global

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

Example:

^test(1)="aa"
^test(2)="aa"
^test(3)="aa"
^test(4)="aa"
Total rows = 4

Product version: Caché 2018.1
$ZV: 5.659.0

Comments

Ed de Moel · Jan 9, 2023

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

0
Jenna Makin · Jan 9, 2023

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

0
Gabriel Santos  Jan 10, 2023 to Jenna Makin

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

^test(1)=""
^test(10)="test"
^test(52)="hello"

The command will return 3

0
Roger Merchberger · Jan 9, 2023

If you have multiple subscript levels, this may help:

SET I=0,G="^test("""")"FOR {SET G=$QUERY(@G) Q:G=""SET I=I+1WRITE 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:

^test(1)
^test(1,1,1)
^test(2)
^test(2,1)
^test(2,2,1)
^test(3,1)
^test(4,1)
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!

0
José Pereira · Jan 9, 2023

Hi!

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^test=4^test(1)="aa"^test(2)="aa"^test(3)="aa"^test(4)="aa"Write^test4

HTH,

José

0
Freddy Baier  Jan 10, 2023 to José Pereira

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^test=2^test(1)="aa"

Exist some command for decrement?

0
Roger Merchberger  Jan 10, 2023 to Freddy Baier

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:

https://docs.intersystems.com/irislatest/csp/docbook/DocBook.UI.Page.cls?KEY=RCOS_FINCREMENT

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)>100W^test,!

Hope this helps!

0
Alexander Koblov · Jan 10, 2023

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

0
Gabriel Santos  Jan 10, 2023 to Alexander Koblov

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

0