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!
Discussion (6)2
Comments
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))"I used it recently in my post on NativeAPI for ObjectScript
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.
or use flag
USER>write "$lb(" _ $listtostring($lb("demo",,123),,7) _ ")"
$lb("demo",,123)Ho interesting! I never noticed the flag argument of the function $ListToString.