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).

SAMPLES>set addr = "One Memorial Drive"

SAMPLES>set city = "Cambridge" ; for both examples below

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

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

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

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

FeedbackOpens in a new tab