Skip to content

Instantly share code, notes, and snippets.

@mhulse
Created April 7, 2011 00:50
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/906837 to your computer and use it in GitHub Desktop.
Save mhulse/906837 to your computer and use it in GitHub Desktop.
Trim whitespace around items in a delimited string. Language keywords: CSP (Caché Server Page), COS (Caché Objectscript)
<script language="cache" method="OnPreHTTP" arguments="" returntype="%Boolean">
do %response.SetHeader("Content-Type", "text/plain")
quit 1
</script>
#[ new string set string = " Top Story , Top Stories , Other Stories , Bulleted Stories " ]#
<csp:comment>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++</csp:comment>
<script language="cache" method="cleanListM1" arguments='str:%String=""' returntype="%List" procedureblock="1">
; Can this be converted to a macro?
; Big thanks to Kev! See his orignal snippet, and comments, here:
; https://groups.google.com/d/msg/intersystems-public-cache/8iJV1p3kwD8/_v21GuBf4iAJ
// Multiline? No problem! Using ##Continue...
// The tricky part is ##Unique:
// http://docs.intersystems.com/cache20101/csp/docbook/DocBook.UI.Page.cls?KEY=GCOS_macros#GCOS_macros_mpp_lblbUnique
// I kept getting a "missing right parenthesis" error when using a "set foo = xxx". :(
/*
#define cleanList(%str) ##Continue
set %str = $listbuild("hello")
quit $$$cleanList(str)
*/
// Note: I have experimented in multiple ways to get a multi-line macro to work but I keep getting errors.
// I will have to re-visit this.
// I have a feeling that there are limitations to using mult-line macros in a CSP page/method.
</script>
#($lts(..cleanListM1(string)))#
<csp:comment>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++</csp:comment>
<script language="cache" method="cleanList1" arguments='str:%String=""' returntype="%List" procedureblock="1">
; Another big thanks goes out to Ian! See his orignal snippet, and comments, here:
; https://groups.google.com/d/msg/intersystems-public-cache/8iJV1p3kwD8/PErk1KzNizMJ
set list = ""
for i=1:1:$length(str, ",") {
set list = list _ $listbuild($zstrip($piece(str, ",", i), "<>W"))
}
quit list
</script>
#($lts(..cleanList1(string)))#
<csp:comment>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++</csp:comment>
<script language="cache" method="cleanList2" arguments='str:%String=""' returntype="%String" procedureblock="1">
; Returns string.
; My first attempt.
set return = ""
set count = $length(str, ",")
for i=1:1:count {
set return = return _ $zstrip($piece(str, ",", i), "<>W") _ $case(i, count:"", :",")
}
quit return
</script>
#(..cleanList2(string))#
<csp:comment>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++</csp:comment>
<script language="cache" method="cleanList3" arguments='str:%String=""' returntype="%List" procedureblock="1">
; Returns $list.
; My second attempt.
; Ian's solution above is much cleaner! :)
set list = $listfromstring(str, ",")
if ($listvalid(list)) {
for i=1:1:$listlength(list) {
set:($listdata(list, i)) $list(list, i) = $zstrip($list(list, i), "<>W")
}
}
quit list
</script>
#($lts(..cleanList3(string)))#
<csp:comment>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++</csp:comment>
#[ kill string ]#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment