Trim whitespace around items in a delimited string

88 views
Skip to first unread message

Micky Hulse

unread,
Apr 6, 2011, 8:56:22 PM4/6/11
to intersystems...@googlegroups.com
Just brainstorming on a couple of ways to trim whitespace around items in a delimited string:

https://gist.github.com/906837

Thinking things could be more compact if I used COS shorthand... I think I would go with the second example in a query using %INLIST.

Kinda wondering if I am being overly cautious by using $listvalid and $listdata... I guess I just want to avoid any <LIST> errors.

Optimally, I would love to throw this into a macro, but I have not had any luck with for loops and macros.

Open to feedback. Is there an easier/cleaner way?

Thanks!
Micky

kevin furze

unread,
Apr 7, 2011, 2:09:51 AM4/7/11
to Caché, Ensemble, DeepSee
Mickey/
try this example
it has a macro called $$$rsetLoopAllToArr
the key word is ##Continue
this allows you to place a macro over multiple lines

#define rsetLoopAllToArr(%arr) ##Continue
s row=0 ##Continue
k %arr ##Continue
While (result.Next()) { ##Continue
s row=row+1 ##Continue
s columns = result.GetColumnCount() ##Continue
f col = 1:1:columns d ##Continue
. s colName=result.GetColumnName(col) ##Continue
. s colData=result.GetData(col) ##Continue
. Set %arr(row,colName)= colData ##Continue
}


if you need to use macro variables then try this
it converts ewdAPI text areas from HTML to a single string.
#define getTextArray(%key,%val) d mergeFromTextArea^%zewdAPI(%key, .x,
sessid) s %val="" s lines=+$g(x(0)) s ##Unique(new)="" f s
##Unique(old)=$o(x(##Unique(old))) q:##Unique(old)="" i
##Unique(old)'=0 s %val=%val_$s(%val="":"",
1:$c(13,10))_x(##Unique(old))

this uses ##Unique but the problem I found is that I never could work
out how to use TWO #Unique's in the same macro and then know which one
is which but you get the idea
you could quite easily replace the variable "i" for ##Unique in your
code.

regards

kev

icargill

unread,
Apr 7, 2011, 3:17:40 AM4/7/11
to Caché, Ensemble, DeepSee
Micky,

I'd be inclined to strip and convert to list in one go, rather than
converting to a list and the processing the list elements.
(But there is no strong reason for doing it that way.) Something
like:

CleanList(string)
Set lst=""
for i=1:1:$Length(string,",")
{
Set sp=$ZStrip($Piece(string,",",i),"<>W")
Set lst=lst_$LB(sp)
}
Quit lst

--
Ian

Micky Hulse

unread,
Apr 7, 2011, 12:23:37 PM4/7/11
to intersystems...@googlegroups.com
Whoa! Kev and Ian... You folks ROCK!

Testing and experimenting with your code snippets now... I will post
back my results in a few (I plan on updating gist with your examples
with full credit and link back to your posts in this thread, I hope
ya'll don't mind?)

Thanks!!!!

BBS,
Micky

kevin furze

unread,
Apr 7, 2011, 2:11:31 PM4/7/11
to Caché, Ensemble, DeepSee
Micky,
a suggestion.
I found it difficult to remember which classes use macros that I
create and consequently, I had to work out which classes needed to be
recompiled whenever I changed/added a new macro

so I created a new class that just included a reference to my macros,
no classmethods, just the "Include" statement - example

Include myMacro
Class macros.myMacro Extends %RegisteredObject
{

}

now whenever I need to access myMacros, I simply use the "extend"
syntax at the top of my new class - example

Class User.myBigClass Extends (%RegisteredObject,
macros.myMacro)
{

ClassMethod myNewMethod()
{
}

}

doing this enables me to re-compile anyclass that references
"myMacros"

one other thing, a macro can call another macro and that can call
another macro (ie nested macros), don't be afraid to experiment - take
a look at some of the system macros, there;'s a lot of good code
examples there, it takes a while to find which system macros are good
examples, start with "large macro files" and work down the files in
"size order"

kev

Micky Hulse

unread,
Apr 7, 2011, 3:29:40 PM4/7/11
to intersystems...@googlegroups.com
Hi Kev! Thanks so much for your help, I greatly appreciate it! :)

See inline replies below.

On Thu, Apr 7, 2011 at 11:11 AM, kevin furze <ke...@oakbeam.co.uk> wrote:
> I found it difficult to remember which classes use macros that I
> create and consequently, I had to work out which classes needed to be
> recompiled whenever I changed/added a new macro
>  so I created a new class that just included a reference to my macros,
> no classmethods, just the "Include" statement - example

> now whenever I need to access myMacros, I simply use the "extend"
> syntax at the top of my new class - example

Ooooh, that's a great idea! Thank you for sharing!

To be truthfully honest, I have only used macros (of my own creation)
in RULEs, for example:

https://github.com/mhulse/custom.rg.ApDateTimeRule/blob/master/custom.rg.ApDateTimeRule.csr

You have inspired me to create a Macro routine so I can experiment and
learn some new stuff.

In fact, I have been getting "missing right parenthesis", and other
errors, when using "set foo = XXXX" and/or ##Continue... I think it is
because I am writing the macro in a CSP page (vs. using studio and
storing my macro as a .mac routine). I am hoping that using Studio and
creating a .mac file will give me a little more freedom and less
CSP/compile errors. :D

> doing this enables me to re-compile anyclass that references
> "myMacros"

That's cool!

I have only ever used "Include myMacro" at the top of the class... I
did not realize I could extend a macro routine, that's slick!

Just out of curiosity, what are the steps you take to re-compile
classes that refernece "myMacros"? Sorry if that is a silly
question... Even though I have been using COS for a few years now,
there is still so much that is new to me. Feel free to point me
towards the manual on this one. :)

> one other thing, a macro can call another macro and that can call
> another macro (ie nested macros), don't be afraid to experiment - take
> a look at some of the system macros, there;'s a lot of good code
> examples there, it takes a while to find which system macros are good
> examples, start with "large macro files" and work down the files in
> "size order"

Awesome! Thanks a billion for all of the tips! :)

Checking system macros now (i.e. changed to the %SYS namespace in
Studio and browsing through the Routines there... Is this what you
mean?)

Thanks Kev!

Cheers,
Micky

--
Micky Hulse
Web Content Editor
The Register-Guard
3500 Chad Drive
Eugene, OR 97408
Phone: (541) 338-2621
Fax: (541) 683-7631
Web: <http://www.registerguard.com>

Micky Hulse

unread,
Apr 7, 2011, 3:55:28 PM4/7/11
to intersystems...@googlegroups.com
Correction:

On Thu, Apr 7, 2011 at 12:29 PM, Micky Hulse <mickyhul...@gmail.com> wrote:
> because I am writing the macro in a CSP page (vs. using studio and
> storing my macro as a .mac routine). I am hoping that using Studio and

That should be ".inc", not ".mac". Sorry about my confusion on the
file extension for ObjectScript macros.

Micky Hulse

unread,
Apr 7, 2011, 7:22:17 PM4/7/11
to intersystems...@googlegroups.com
On Thu, Apr 7, 2011 at 9:23 AM, Micky Hulse <mickyhul...@gmail.com> wrote:
> Testing and experimenting with your code snippets now... I will post
> back my results in a few (I plan on updating gist with your examples
> with full credit and link back to your posts in this thread, I hope
> ya'll don't mind?)

I just posted some updated code:

https://gist.github.com/906837

I was never able to get multi-line macro to work in that context (CSP
page/CSP methods/CSP script).

I am currently in the process of creating/writing my first INC file
(and class that extends that mac file) so I am hoping that will give
me the multi-line and variable functionality that a CSP file does not.

Thanks again! I will post back my results.

I really appreciate the help Kev and Ian!

Cheers,
Micky

kevin furze

unread,
Apr 8, 2011, 2:23:59 AM4/8/11
to Caché, Ensemble, DeepSee
Micky
How to compile your macros?
quick and easy, when you know ;-)
choice of a couple of ways.
open up the system portal, click on [classes], click on [compile],
tick the box [compile dependant classes] and then pick your
macros.myMacros class and GO

alternative way,
using studio
click on [OPTIONS], click on [compiler], click on [compile dependant
classes] , now open up your macros.myMacros and re-compile the class,
all the classes that use that one gets recompiled.

some system macros,
LANGF11ovr.inc
( this one includes calls to other .inc files), uses ##continue etc.,
use of macro functions - read the docs etc)

ROLLBACK.inc
%qMVCopyright (this is where the copyright notice comes from at the
top of all of your compiled classes)
(not the use of ##IF 0 - effectively "never true, never included, so
becomes a comment")

have a look, they may not be easy to follow, but they give good
concepts that you may be able to follow.

regards

kev

Micky Hulse

unread,
Apr 8, 2011, 2:35:02 PM4/8/11
to intersystems...@googlegroups.com
Hi Kevin!

On Thu, Apr 7, 2011 at 11:23 PM, kevin furze <ke...@oakbeam.co.uk> wrote:
> quick and easy, when you know ;-)

> ...<snip>...

Oh, that sounds too easy!!!

Thanks so much for the pro tips! I can't wait to experiment. :)

> some system macros,
> LANGF11ovr.inc
> ROLLBACK.inc
> ...<snip>...


> have a look, they may not be easy to follow, but they give good
> concepts that you may be able to follow.

You ROCK! Thanks so much Kevin (and Ian), I really appreciate all of
your help! :)

Have an excellent weekend!

John Bertoglio

unread,
Apr 10, 2011, 10:56:53 AM4/10/11
to Caché, Ensemble, DeepSee
Damn clever Kevin. Do you know if this is a supported syntax? This is
the first example I have ever seen of a multi-line macro. Clearly,
this would expand the usefulness of an already useful technique but I
would hate to find my system dead because a .x release killed all my
multiline macros...:)

jb

kevin furze

unread,
Apr 10, 2011, 12:49:41 PM4/10/11
to Caché, Ensemble, DeepSee
John,
I'm assuming you are talking of the ##Continue syntax.
glad you like this one

just checked the latest 2010.2 docs over at intersystems, its still a
fully supported syntax,
I've been using it for years and never had problems with support and I
can't imagine intersystems dropping it, they use it themselves.

one thing to note,
older mumps programmers make use of the dot block syntax eg:
if x=1 d
. s y=4
. s y
. s z=5
. d xPlusy(x,y)

this works fine, but if you start playing with multi-line macros, you
risk breaking the dot structure as macros have no way of knowing how
many dots you currently have in your block (1 dot deep, 2 dots
deep ? ) and how many dots are you going to generate?
so if you intend (and why wouldn't you) plan to make a truly universal
macro, make sure you use the curly bracket syntax eg { } on your "for
loops" or "do" or "while" etc within the macros or make sure your
macro fits on one line

If you want to do really clever things, look at ##function and others,
all within the same docs page

take a look at
http://docs.intersystems.com/cache20102/csp/docbook/DocBook.UI.Page.cls?KEY=GCOS_macros#GCOS_macros_mpp_lblbContinue
for the current syntax

theres a few very interesting ideas within intersystems "system
macros", I've picked up a lot just by snooping at the source macro
codes

kev
Reply all
Reply to author
Forward
0 new messages