Length of subcript of array or global variable
Are there any direct command to find the length of subscript of global variable? Is it possible to code in objectscript to find the maximum length of subscript in a global variable.
Product version: Caché 2018.1
Hello Nikhil,
What problem are you trying to solve or what exactly are you trying to do? See the below docs:
"The maximum length of a subscript is 511 encoded bytes (the corresponding number of characters depends on the characters in the subscript and the current locale)"
https://docs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?KEY=GCOS_variables#GCOS_variables_subscripts
Maximum Length of a Global Reference
You can use $length to get the length of a string, but depending on what you want to do, that may determine how you can get the value of the subscript.
https://docs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?KEY=RSQL_d_length
I'm not sure if I understand your first question - if you're asking if there's a single "$command" that will just give you a length of a subscript? I don't think so, as you can have multiple subscripts, and I don't think there'd be a command that would let you pick which one you wanted output. However, if you're asking can you get a length of a subscript in a single line of code, then you certainly can - M is quite powerful with what you can do at the command prompt without utilizing Studio/Atelier. Imagine you have these globals:
S ^ZTEST("ME")=1 S ^ZTEST("YOU")=2 S ^ZTEST("THEM")=3 S ^ZTEST("OTHERS")=4
If you run this one line:
you'll get the output of each of the subscripts and the length of each:
SUB: ME WITH LENGTH OF: 2 SUB: OTHERS WITH LENGTH OF: 6 SUB: THEM WITH LENGTH OF: 4 SUB: YOU WITH LENGTH OF: 3
The F is a FOR loop, the $O(RDER) can retrieve the next or previous subscript, and the $L(ENGTH) function gives you the length.
For the 2nd half of your question, you surely can test maximum length of a subscript using a Try/Catch test. Try this code:
ZSUBTEST ; TEST LENGTH OF SUBSCRIPT. N I,I2,I3,J,J2,J3 S I=1,J="B",J2=0 DO { TRY { S ^ZTEST(J)="YUP" S I=I+1,J=J_"A" ; W I,*9,J,! } CATCH { W "A "_$ZERROR_" occured, caused by a subscript of "_I_" characters.",! S J2=1 } } WHILE 'J2 Q
You should see the output:
A <SUBSCRIPT>ZSUBTEST+6^ZSUBTEST ^ZTEST("BAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAA occured, caused by a subscript of 505 characters.
Or something similar. The program simply starts with a subscript one character long and keeps adding a character until the error occurs (caught by the CATCH part of the code) and displays the length of the subscript it couldn't handle.
Hope this helps!
You may also use $QS and $QL functions to calculate the number of subscripts and lengths of a global reference
Take a look at the ObjectScript functions $NAME, $QUERY, $QSUBSCRIPT and $QLENGTH
https://docs.intersystems.com/irislatest/csp/docbook/Doc.View.cls?KEY=RC...
Yes, but it is unsupported. The expression $ZUTIL(70,2,value) will return value encoded for use as a subscript subject to the current default subscript encoding. You can combine this with $LENGTH(), so $LENGTH($ZUTIL(70,2,value)) to get the length of a subscript once encoded. This technique should never find its way into production code. However, if you just want to understand how various codepoints are encoded, you can use it for experimentation.