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

  • 0
  • 0
  • 762
  • 4
  • 2

Answers

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"

Comments

very slick....for you old Mumpsters, here's an extrinsic function

%Split(List,Array,del) ;;liberating a VB function
del=$G(del,"|")
Do ##class(%ListOfDataTypes).BuildValueArray($lfs(List,del),.Array)
1

I suppose the problem is my cache version.

But i can't compile the code with the line: "%Split(List,Array,del) ;;liberating a VB function"

Btw nice point on Mumpsters lol

Maybe the lack of line indent in Marc's post confused you. Try this:


%Split(List,Array,del) ;;liberating a VB function
 S del=$G(del,"|")
 Do ##class(%ListOfDataTypes).BuildValueArray($lfs(List,del),.Array)
 Q 1