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

String Variables

GHS is fundamentally a language. This ensures generality (hence the namesake "G") and it is also what makes the software incredibly powerful. However, it also means that being a GHS super-user is often about building a familiarity with the syntax, and knowing the tricks.

In COW134 we proposed the powers of READ (DATA). In COW146 we offered some examples of the SET command. And in COW154 we discussed array variables. While these COWs are perhaps not our most exciting work, you may have noticed some syntactic maneuvers that went unmentioned--specifically the use of "strings" and string variables.

What are strings? In brief, they are the non-numeric assignments: things like characters, words, "strings" of letters and numbers, even numbers alone if you change the way you think about them. Although string variables are a specific type (i.e. you can create them with VARIABLE (STRING), as in COW134), a simple VARIABLE in GHS can often serve double-duty, with GHS interpreting the type implicitly in most cases (parsing is one case where it's best to be explicit).

We can leverage this fact when using strings to pass a variable name as a parameter, and not just the variable assignment. We can then access the variable by using the string name parameter:

VARI A
MACRO add
 SET %3=%1 PLUS %2
/

.add 4 3 "a"

\ Result: {a} \ `returns 7

And of course, the same can be done with macros:

MACRO operation
 .%4 %1 %2 "%3"
/

.operation 4 3 "a" "add"

\ Result: {a} \ `returns 7

...which will pass the macro name add as the fourth parameter of the macro operation. One could define other macros like add and switch the functionality of operator by simply passing the name of a different macro. These concepts are simple but immensely powerful.

To illustrate, consider the following example. This macro accepts any simple symbolic mathematical expression as a string, and then parses and evaluates this expression with clever string manipulation and SET commands, before returning the result in the user variable defined by the second parameter:

MACRO eval
 VARIABLE len,k,ii
 VARIABLE (STRING) upto,after,exp,c
 MACRO replace_ops
  SET ii={ii} PLUS 1
  SET c=LEFT {ii} "{exp}"
  SET c=RIGHT 1 "{c}"
  SET k={ii} MINUS 1
  SET upto=LEFT {k} "{exp}"
  SET k={len} MINUS {ii}
  SET after=RIGHT {k} "{exp}"
  IF "{c}"="-" THEN SET exp="{upto} MINUS {after}"
  IF "{c}"="+" THEN SET exp="{upto} PLUS {after}"
  IF "{c}"="*" THEN SET exp="{upto} TIMES {after}"
  IF "{c}"="/" THEN SET exp="{upto} DIVIDE {after}"
  SET len=CHARS "{exp}"
  IF {ii}>{len} THEN EXIT
  EXIT replace_ops
 //
 SET exp="%1" | SET len=CHARS "{exp}"
 SET ii=0
 .replace_ops
 SET %2={exp}
 CLEAR (len) VARI | CLEAR (k) VARI | CLEAR (ii) VARI
 CLEAR (upto) VARI | CLEAR (after) VARI
 CLEAR (exp) VARI | CLEAR (c) VARI
/

VARIABLE ans

.eval "5*5+34.32-1" ans

\ Result: {ans} \ `returns 58.32

It might be a convenience over the quirky SET command we can't seem to stop talking about, but like the SET command, expressions are still evaluated from left to right. That's not to say that you couldn't extend it to respect PEMDAS.

My dear Aunt Sally, you're not excused. When did COWs become lessons in arithmetic?

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

support@ghsport.com

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

www.ghsport.com

Click here for an index to this and previous COWs