Written by

Solution Architect at Zorgi
MOD
Question Lorenzo Scalese · Aug 26, 2023

How to store $lb representation in a string ?

Hi !

I'm looking for a simple way to store in a string a list in $lb representation.  Ex : 

Set list = $ListBuild("data2", "data2", "data3")
ZWrite list
/* show : list=$lb("data1","data2","data3") */

With the ZWrite command, we can display the $lb representation but I would like to store it in a string variable.

I can do it with this code : 

Set strtp = """%1""", str = "$lb(", ptr = 0While$ListNext(list, ptr, value) {
    Set str = str _ $Select($IsValidNum(value): value, 1: $Replace(strtp,"%1",value)) _ ","
}
Set$Extract(str,*) = ")"Write !,str
/* show : $lb("data1","data2","data3") */

Is there an easier way with a pre-built function/method?

Thank you!

Comments

Robert Cemper · Aug 26, 2023

Hi @Lorenzo Scalese 
I guess you are looking for class(%Utility).FormatString()

USER>set lb=$lb(1,"Lorenzo",2023,"RCC"_$c(13,10))
USER>write lb
      LorenzoçRCC
 
USER>zzdump lb
0000: 03040109014C 6F 72656E 7A 6F 0404 E7 07         .....Lorenzo..ç.
0010: 07015243430D0A                                    ..RCC..
;;;;;  this is it
USER>set viewlb=##class(%Utility).FormatString(lb)
 
USER>write viewlb
$lb(1,"Lorenzo",2023,"RCC"_$c(13,10))
USER>zwrite viewlb
viewlb="$lb(1,""Lorenzo"",2023,""RCC""_$c(13,10))"
0
Rich Taylor · Aug 28, 2023

This command will do this also:

set str ="$lb("""_$replace($listtostring(x),",",""",""")_")"

There is a caveat though,  If the list is empty the $listtostring() will through a NULL exception. So you will need to check for an empty string.

0
Dmitry Maslennikov  Aug 28, 2023 to Rich Taylor

or use flag

USER>write "$lb(" _ $listtostring($lb("demo",,123),,7) _ ")"
$lb("demo",,123)
0