Skip to content

Instantly share code, notes, and snippets.

@mhulse
Last active December 19, 2015 02:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mhulse/5886459 to your computer and use it in GitHub Desktop.
Save mhulse/5886459 to your computer and use it in GitHub Desktop.
Caché 2009.1: What's the best way to remove empties from a list and what's a good way to flip a list?
#[ set string = "dog goes ""bark"", duck says ""quack"", goose makes ""honks"", ,,,,,,,,,,,,,,,,cow goes ""moooo"", pig ""snorts"",,,The quick brown fox jumps over the lazy dog,," ]#
<p>String: #(string)#</p>
<script language="cache" runat="server">
; Initialize:
set delimiter = ","
set list = ""
set reverse = 1 // ... or "0" (boolean).
; Reverse the string:
set:(reverse) string = $reverse(string)
; Loop over string using provided delimiter:
for i=1:1:$length(string, delimiter) {
; Grab current item and trim the whitespace:
set temp = $zstrip($piece(string, delimiter, i), "<>W")
; Reverse mode?
set:(reverse) temp = $reverse(temp)
; If there's something left over, add it to the return value:
set:($length(temp)) list = list _ $listbuild(temp)
}
</script>
<p>Result: "#($listtostring(list))#"</p>
<p>Empties are gone and the list has been reversed, but is there a better way?</p>

String: dog goes "bark", duck says "quack", goose makes "honks", ,,,,,,,,,,,,,,,,cow goes "moooo", pig "snorts",,,The quick brown fox jumps over the lazy dog,,

Result: "The quick brown fox jumps over the lazy dog,pig "snorts",cow goes "moooo",goose makes "honks",duck says "quack",dog goes "bark""

Empties are gone and the list has been reversed, but is there a better way?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment