Skip to main content

Compare Pieces and Lists

Earlier, you learned about $Piece and delimiters, which also provides list-like functionality. A list is useful when you want to avoid thinking about delimiters altogether. With delimiters, there's always the chance that one of the substrings will contain the delimiter character, which will change the positions of the rest of the pieces in the string. Lists prevent this.

Let's look at the two approaches side-by-side. Each piece-oriented function has an analogous list-oriented function, and the difference is the absence of a delimiter. $ListFind is more exact than $Find in this context, as you can see. Although you can Write Lmail (built with $ListBuild), it's not recommended, because it contains control characters. You should access lists solely using the special list functions. When working with lists using the Terminal, you can use the ZWrite command to display the list contents (in a simple variable, an array, or a global).


USER>set addr = "One Memorial Drive", city = "Cambridge" ; for both examples below

USER>set st = "MA", zip = "02142" ; for both examples below

USER>set Pmail = addr _ "^" _ city _ "^" _ st _ "^" _ zip

USER>set Lmail = $listbuild(addr, city, st, zip)

USER>write $piece(Pmail, "^", 2), ?20, $list(Lmail, 2)
Cambridge           Cambridge
USER>write $length(Pmail, "^"), ?20, $listlength(Lmail)
4                   4
USER>write $find(Pmail, "MA"), ?20, $listfind(Lmail, "MA")
32                  3
USER>write Pmail
One Memorial Drive^Cambridge^MA^02142
USER>zwrite Lmail
Lmail=$lb("One Memorial Drive","Cambridge","MA","02142")
FeedbackOpens in a new tab