General HydroStatics
Ship Stability Software
Command of the Week
(New or interesting aspects of GHS that you may not know about)

String Variable (part 2) - Concatenate

John Bonn wrote in with a follow-up question to last week's COW. He wanted a simple explanation of how to concatenate strings.

"Oh, that's easy," we said. "Just put two or more strings together surrounded by quotes. No special characters are needed to join strings or add spaces."

Here are two examples:
`simple concatenation
set a="hello"
set b="world"
set program_message="{a} {b}"

`creating a damage case
set tank1="forepeak.c"
set tank2="engrm.c"
set tank3="db0.s"
set dc1="{tank1}, {tank2}"
set dc2="{tank2}, {tank3}"
set dc3="{dc1}, {dc2}"

But wait, what about duplicates within dc3? Well, the TANK command allows them and so does the TYPE command, so the main reason to avoid duplication is for printing the damage case in the report. One solution is to write a macro that removes duplicates from a string of items. A simpler solution is:
set dc3="{tank1}, {tank2}, {tank3}"

"OK, I get it, but what if I want to have quotes within a string?" John emailed later that day.

"Well, the rule is simple: just double any quotes you want to appear within the string. If you don't like how that looks, use the QUOTE function of the SET command instead. The same is true for template labels as well."
set label="this string contains a ""quote"""     `or
set label=QUOTE this string contains a "quote"
template t

Nearly satisfied, John had one more question. "What if I want to deconstruct a series of items all stored in the same variable?"

"That is a perfect use for the ITEM operator in the SET command. I'm glad you thought to ask. It is nearly the opposite of concatenating strings. It copies the nth item in a list separated by commas, quotes, spaces."
MACRO Itemize
 set n={n} plus 1
 SET temp=item {n} "{%1}"
 if "{temp}"="" then exit
 variable Item{n}="{temp}"
 exit Itemize %1
variable n=0, temp
.Itemize dc3
clear (temp) variable
variable /list:item*

"Boy, you guys do a great job on these COWs," complimented John. "I look forward to reading the next edition each week."

"Thanks John. We were lucky enough to have Labor Day off to work on this one."

Questions, comments, or requests?
Contact Creative Systems, Inc.

USA phone: 360-385-6212 Fax: 360-385-6213
Office hours: 7:00 am - 4:00 pm Pacific Time, Monday - Friday

Mailing address:
PO Box 1910
Port Townsend, WA 98368 USA

Click here for an index to this and previous COWs