NewBie's Corner Session 2 Variables Set and Write commands

NewBie's Corner Session 2 Variables Set and Write commands

Welcome to NewBie's Corner, a weekly or biweekly post covering basic Caché Material.

Session 2 – Click on the Caché Cube in your system tray and select Terminal to try out the commands.


Like other computer programming languages, Caché uses Variables and the values they represent to control programming. Variables are elements that represent data values. Manipulating and interrogating variables is at the root of programming.

  • Variable names are case-sensitive
  • Variable names should be no longer than 31 characters
  • Variable names must be either Alpha or Numeric characters. No underscores or dashes
  • Variables must start with either "%" or an alpha character (A, B, C . . . Z), lower or upper case
  • By convention, variable names that start with "%", are reserved for the system

Set command

The Set command is the primary method of assigning values to variables.

Unlike Variables, commands are not case sensitive.

Set X=12
Set X="ABC"

Here, variable X is first set to 12 and then to "ABC" using the Set command. Notice that when setting a variable to a numeric value, no quotation marks are necessary.  However, when setting a variable to an alphanumeric value, double quotation marks are required.

Write command

The Write command displays the value of variables.

Set X=12
Write X
Set X="ABC"
Write X

The Write command displays the value of variable X. When variable X is set to 12, the Write command displays 12, and when the variable X is set to "ABC" (using quotes), ABC is displayed.

Display all variables 

A Write command used with no subsequent variables, displays all defined variables (in alphabetic order).

Set Z=14
Set X=12
Set Y=13
Write                     ; Write command with no variables

Note, after the semi-colon, comments may be entered.

Further notes on Variable Names

  • Variable names may be longer than 31 characters, but only the first 31 significant characters are used, and longer variable names become cumbersome.
  • Many developers use variables such as X, Y, and Z as temporary variables. Also, variable I is typically used as an Index variable.
  • If you like your variable names to represent meaningful phrases, try capitalizing the first letter of each word, for example:
    • MyStartingValue
    • CountMyInvoices
  • The key in your variable names (as in every other part of programming) is understandability, can a developer looking at your code, know what your variable names mean?

Further notes on Commands

Some Caché developers only use the first character of a command. Hence, "S" is used for Set and "W" is used for Write. This may be a hold-over from earlier MUMPS code when the length of a line was important. The one character commands tend to make code look cryptic, but others may disagree.

--Mike Kadow

If you have a comment, please respond through the InterSystems Developer Community, don't send me private email. Unless of course you wish to address me only.

See NewBie Index for an index of all NewBie Corner posts.

  • + 2
  • 0
  • 208
  • 0