Question
· 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 = 0
While $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!

Discussion (6)2
Log in or sign up to continue

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: 03 04 01 09 01 4C 6F 72 65 6E 7A 6F 04 04 E7 07         .....Lorenzo..ç.
0010: 07 01 52 43 43 0D 0A                                    ..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))"