Question
· Jan 12, 2017

String manipulation

Curious if there is a string function which can transform a delimited string into an array without looping through $Piece...such as "a,b,c,d,e,f" into array(0)=5,array(1)="a",array(2)="b" etc

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

There are not such methods, we only have function which converts delimited string to $listbuild format and back. And you can do it in this way, without any looping, but only if you already know how many items you have.

USER>set string="a,b,c,d,e,f"

USER>set list=$lfs(string,",")

USER>zw list
list=$lb("a","b","c","d","e","f")

USER>set $lb(array(0),array(1),array(2),array(3),array(4),array(5))=list

USER>zw array
array(0)="a"
array(1)="b"
array(2)="c"
array(3)="d"
array(4)="e"
array(5)="f"

# 1
##class(%ListOfDataTypes).BuildValueArray($lfs("a,b,c,d,e,f"),.array)
array(0)=$o(array(""),-1)
zw array

# 2
list=##class(%ListOfDataTypes).%New()
list.InsertList($lfs("a,b,c,d,e,f"))
array=list.Data
array(0)=list.Count()
zw array

The Result for both variant:
array(0)=6
array(1)="a"
array(2)="b"
array(3)="c"
array(4)="d"
array(5)="e"
array(6)="f"