Skip to main content

Setting Substrings, Pieces, and List Items

You've seen that you can use $Extract, $Piece, and $List to retrieve parts of strings. You can also use these three functions along with Set to set substrings into strings. All the examples start by showing the use of the functions on variables that don't exist yet.

Set $Extract will pad on the left with spaces if you use it to set a substring in the middle of an empty string. It extracts the characters specified and replaces them with the supplied substring, whether or not the lengths of the old and new strings match.

SAMPLES>set $extract(empty, 4) = "abcde"

SAMPLES>write empty
   abcde
SAMPLES>set $extract(empty, 4, 5) = "12345"

SAMPLES>write empty
   12345cde
SAMPLES>

Set $Piece will add enough delimiters so that it can place the substring at the proper piece of an empty string. A substring can also have pieces, using a different delimiter. You can nest $Piece functions in order to retrieve subpieces.

SAMPLES>set $piece(empty, "^", 3) = "abcde"  write empty
^^abcde
SAMPLES>write $length(empty, "^")
3
SAMPLES>set $piece(empty, "^", 2) = "9/9/1999"  write empty
^9/9/1999^abcde
SAMPLES>write $piece( $piece(empty, "^", 2), "/", 3)
1999
SAMPLES>

Set $List will add enough list items so that it can place the substring as the proper item in an empty list. A list item can also be a list. You can nest $List functions in order to retrieve sublists.

SAMPLES>set $list(empty, 3) = "abcde"

SAMPLES>write $listlength(empty)
3
SAMPLES>set $list(empty, 2)=$listbuild(9, 9, 1999)

SAMPLES>write $list( $list(empty, 2), 3)
1999
SAMPLES>
FeedbackOpens in a new tab