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.

Terminal


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

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

USER>write empty
   12345cde
USER>
Copy code to clipboard

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.

Terminal


USER>set $piece(empty, "^", 3) = "abcde"  write empty
^^abcde
USER>write $length(empty, "^")
3
USER>set $piece(empty, "^", 2) = "9/9/1999"  write empty
^9/9/1999^abcde
USER>write $piece($piece(empty, "^", 2), "/", 3)
1999
USER>
Copy code to clipboard

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.

Terminal


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

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

USER>write $list($list(empty, 2), 3)
1999
USER>
Copy code to clipboard
FeedbackOpens in a new window