Multi-line string variable?

1,198 views
Skip to first unread message

Micky Hulse

unread,
Jun 26, 2013, 7:02:51 PM6/26/13
to intersystems. public. cache
Hello,

This is such a beginner question ...

What's the recommended way to set a multi-line string variable?

The obvious way I can think of is:

set foo = "first line" _
$char(10) _
"second line" _
$char(10) _
"third line"

Is there a better way to do the above?

For example, something similar to PHP's heredoc or Python's triple
quoted strings.

I feel like I should know this.

Other than the above, is there a better way to setup a multi-line
string variable?

Thanks!
M

Tom Fitzgibbon

unread,
Jun 26, 2013, 7:57:40 PM6/26/13
to intersystems...@googlegroups.com
Mickey -

Lots of ways to do this but I typically use $listbuild to create a variable/global to keep the separate lines without worrying about delimiters.  Then use $list to extract the lines and add whatever control characters at that point.

s x=$lb("abc","more here","end of line") then f i=1:1:3 w $list(x,i),$c(13,10)

Hope it helps.

- Tom Fitzgibbon | Multidata | 212-967-6700 x537



--
--
Caché, Ensemble, DeepSee

---
You received this message because you are subscribed to the Google Groups "Caché, Ensemble, DeepSee" group.
To unsubscribe from this group and stop receiving emails from it, send an email to intersystems-publi...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



Tom Fitzgibbon

unread,
Jun 26, 2013, 7:58:25 PM6/26/13
to intersystems...@googlegroups.com
Sorry, Micky, misspelled your name. - Tom

Micky Hulse

unread,
Jun 26, 2013, 8:09:40 PM6/26/13
to intersystems. public. cache
Hi Tom!

On Wed, Jun 26, 2013 at 4:58 PM, Tom Fitzgibbon <t...@mul.com> wrote:
> Sorry, Micky, misspelled your name. - Tom

No worries! Happens all the time. Micky is usually spelled with an "e"
anyway. :D

> On Wed, Jun 26, 2013 at 7:57 PM, Tom Fitzgibbon <t...@mul.com> wrote:
>> Lots of ways to do this but I typically use $listbuild to create a
>> variable/global to keep the separate lines without worrying about
>> delimiters. Then use $list to extract the lines and add whatever control
>> characters at that point.

That's good info, thanks so much Tom! I'll use that technique in the future.

Just curious, but why $c(13,10)? Wouldn't $c(10) suffice?

With that said, I probably should have given a reason as to why I want
a multi-line string.

I'm trying to replicate what I'd get back from my database.

For example, a user might type-in:

[snip]

Hello, this is a line.
This is a new line.
This is a third line.

[/snip]

We have code that can extract that value from the DB and assign it to
a variable.

I'm writing a plugin, and I'd like to emulate what I'd get out of the
database without actually having to use the database. :D

I figure using the $c(10) (or $c(10, 13)) would do the trick (to
emulate the line returns), but I'd love to setup a test variable like
(for example):

foo = """
this is a line
this is a line
this is a line
"""

Thanks again for the help/tips!

Tom Fitzgibbon

unread,
Jun 26, 2013, 8:48:34 PM6/26/13
to intersystems...@googlegroups.com
Micky -

If this is a Cache DB then obviously you can store/retrieve the data with any or no explicit delimiters.  Non-Cache DB might have it's own special delimiters, none, etc.  Either way you would mimic what the DB does in Cache code.

Something like: f i=1:1 r x q:'$l(x)  s $list(mystring,i)=x w !

$c(13,10) or $c(10) are typical delimiters depending on your storage/retrieval.  If you get to the display/print side use of control characters (CR-13, LF-10, ETX-3, etc) would cause different results based on characteristics of those devices.  $c(13,10) goes all the way back to old printers (carriage return, the line feed).

Since I see you on this newsgroup a lot, you're probably asking something I'm not getting.

- Tom 


Micky Hulse

unread,
Jun 27, 2013, 12:13:25 AM6/27/13
to intersystems. public. cache
Hi Tom! Thanks again for the pro help, I really appreciate it. :)

On Wed, Jun 26, 2013 at 5:48 PM, Tom Fitzgibbon <t...@mul.com> wrote:
> If this is a Cache DB then obviously you can store/retrieve the data with
> any or no explicit delimiters. Non-Cache DB might have it's own special
> delimiters, none, etc. Either way you would mimic what the DB does in Cache
> code.
> Something like: f i=1:1 r x q:'$l(x) s $list(mystring,i)=x w !

Thank you for providing example code for me to play with.

Honestly, my shorthand skills are not the sharpest ... I hate to admit
it, but I'll have to convert your code to long hand in order for me to
fully understand what you're doing (I actually enjoy converting
shorthand COS, as it's good practice for me).

With that said, I think I see what you're saying here ... And yes, I
am pulling from a Caché DB.

Details: I'm using someone else's code, so I'm not sure what
delimiters they have set and/or are using (I think at the deepest
level, it's an XML feed that gets translated and stored as a character
stream). Long story short, once I get my hands on the content/string
(at the web/template level) I can assign it to a variable.

> $c(13,10) or $c(10) are typical delimiters depending on your
> storage/retrieval. If you get to the display/print side use of control
> characters (CR-13, LF-10, ETX-3, etc) would cause different results based on
> characteristics of those devices. $c(13,10) goes all the way back to old
> printers (carriage return, the line feed).

Awesome, thanks for the detailed explanation, that's very helpful.

> Since I see you on this newsgroup a lot, you're probably asking something
> I'm not getting.

Oh, no, I think you get what I'm saying. In fact, I probably could
have been more clear. Sorry if there has been any confusion.

It looks like using $char(13, 10), and working with your examples,
will get me what I need (and then some).

OFF-ish TOPIC & shameless plug:

Here's the code that prompted my question:

<https://github.com/registerguard/delim>

... in particular, my demo page code:

<https://github.com/registerguard/delim/blob/master/delim/delim.csp>

See where I'm setting "string" variables? Specifically:

<https://github.com/registerguard/delim/blob/master/delim/delim.csp#L162-L165>

Well, that's my way of pretending that the value of the string is
coming from our Caché DB.

For some reason, before talking to you, I was thinking that using
$char(10) wasn't going to be a good test case ...

Now, based on what you've said, it seems like using $char(10) is the
closest I'll get to mimicking the output from our DB (other than
actually using the DB).

On top of all that, in general, I was also curious to know if Caché
has something similar to PHP's heredoc and/or Python's triple-quoted
strings (sounds like there isn't anything similar).

Anyway, thanks again Tom! Sorry to chat your ear off.

Have a great night.

Cheers,
Micky

Tom Fitzgibbon

unread,
Jun 27, 2013, 2:12:25 PM6/27/13
to intersystems...@googlegroups.com
Micky - Glad to be of help.  I'm off on something else but good chatting with you. - Tom


icargill

unread,
Jun 27, 2013, 4:32:00 PM6/27/13
to intersystems...@googlegroups.com
Mickey
 
Two answer two points in the thread:
 
a.  On Unix, lines are usually delimited by $Char(10), on Windows, it's $Char(13,10).
 
b. If you put all your lines in a ListBlock (say lb), a simpler way to convert them would be:
 
set str=$LISTTOSTRING(lb , $C(13,10))
 
i.e.  convert list to string, and override default separator of "," with second argument.
 

 

Micky Hulse

unread,
Jun 28, 2013, 1:57:35 PM6/28/13
to intersystems. public. cache
Hi Ian! Sorry for the late reply.

On Thu, Jun 27, 2013 at 1:32 PM, icargill <ian.c...@e-dendrite.com> wrote:
> a. On Unix, lines are usually delimited by $Char(10), on Windows, it's
> $Char(13,10).

On our system, $c(10) works when parsing the actual string from the
database, so I'll stick to using that for now.

Thank you for the clarification! That's good to know. :)

> b. If you put all your lines in a ListBlock (say lb), a simpler way to
> convert them would be:
>
> set str=$LISTTOSTRING(lb , $C(13,10))
>
> i.e. convert list to string, and override default separator of "," with
> second argument.

Awesome, that does seem much easier than using the concat syntax.
Converting my demo code now.

Thanks again Ian (and Tom), I really appreciate the pro help and advice. :)

Have an excellent day.

Cheers,
Micky

Micky Hulse

unread,
Jun 28, 2013, 2:45:46 PM6/28/13
to intersystems. public. cache
On Fri, Jun 28, 2013 at 10:57 AM, Micky Hulse
<mickyhul...@gmail.com> wrote:
> Awesome, that does seem much easier than using the concat syntax.
> Converting my demo code now.

Ahhh, that is much better:

<https://github.com/registerguard/delim/blob/f2f08f0713786eaf5b25e1b2d2f6a9e7b875280f/delim/delim.csp#L282-L289>

[snip]

set string = $listtostring(
$listbuild(
"http://www.youtube.com/watch?v=JJI4WBSttA4 | First video",
"http://vimeo.com/68047477 | Second video",
"http://www.youtube.com/watch?v=wMxmWH5lR3o"
),
$char(10)
)

[/snip]

I love that COS is not sensitive to white space for when it comes to
newlines and function parenthesis.

Thanks again Tom and Ian!!! I owe you one.

Have a nice day.

Cheers,
Micky
Reply all
Reply to author
Forward
0 new messages