MUMPS Programming/Printable version

From Wikibooks, open books for an open world
Jump to navigation Jump to search

MUMPS Programming

The current, editable version of this book is available in Wikibooks, the open-content textbooks collection, at

Permission is granted to copy, distribute, and/or modify this document under the terms of the Creative Commons Attribution-ShareAlike 3.0 License.


MUMPS Terminology[edit | edit source]

First, some MUMPS terminology-

  • Routines - the term for programs, lines of commands in sequence.
    • Each routine has a name (RoutineName) which is used to execute it by way of the DO command.
    • An up arrow ^ (caret character) is a prefix to the RoutineName distinguishing it as a routine versus a label within the current routine.
    • Traditionally a routine is about a page in length as a logical section that is easily visible as a cognitive unit.
  • Globals are the persistent database variables in MUMPS. These are a unique feature.
    • Global Names begin with up arrow (caret) also.
    • Global names have a comma-separated list in parentheses of subscripts as a multi-dimensional array.
    • Subscripts create a hierarchical database, which may be huge, may be shared among many simultaneous tasks.
    • Globals are persistent on the Hard Drive (or other devices like flash drives) as they are the database representation in MUMPS.
    • The intrinsic nature of globals in the MUMPS language largely hide the necessity of application programmers dealing with issues of file opening, buffering, caching, encryption, compression, etc. These are managed with historic sophistication by the binary tree encoding and dynamic creation of the MUMPS system. See MUMPS as an Operating System.
    ^PtData(ClinicId,PtNum,VisitNum,NoteNum,Line)=  ...  
    ;where  data is separated by Clinic first, then by patient, then visit, then note,
    ;   and the node itself contains the text for one line of the patient note.

Beginning MUMPS

GTM>write "hello world"
hello world


Basic MUMPS Syntax - Lines, Spaces, WhiteSpace, Comments[edit | edit source]

MUMPS syntax was designed to be concise in a time of scarce memory. This serves a purpose now in that programs are cognitively concise: a lot more fits in view on a screen or on a page of paper.

  • The basic language structure is command command-separator argument.
    • The command may be one of the MUMPS commands, like "SET" or "WRITE"
    • The command-separator is written as a single space (or blank) between command (SET) and the arguments.
    • The argument depends on the command.
      • The argument can be a list of single-arguments (separated by commas) or a single-argument.
      • Each single-argument usually is in the form of an expression or group of expression, separated by colons.
  • separating each command may be multiple spaces for readability.
  • Another command may follow a space on the same line.
        SET X=1    SET Y=2
  • Whitespace - eol is NOT treated as equivalent to space. Each has different role in MUMPS.
  • Tabs are also NOT equivalent to a space, but are sometimes treated like space to delineate a label. Traditional MUMPS editors (IDEs) sometimes treat tabs specially, but do not put the tab character into the MUMPS routine.
  • Lines are significant to the syntax as each line starts with a tag(line label) and a space or a space when there is no label.
  • Comments start with semicolon (;) and go to the end of the line. A line may start with ; and is all comment.
  • There is no command termination character (like ; in java or Perl) other than the eol or a space following the argument.
  • Assignment requires the explicit command SET or S
        SET X=1,Y=2  ;   either form full or abbreviated command S
        S X=1,Y=2    ;  
  • There is no syntactic role for braces {}
  • Expressions are grouped by parenthesis
  • Programs ('routines' in traditional MUMPS terminology) have a unique name and consist of a linear sequence of lines.


Variables are names that refer to values. In MUMPS, there are are two kinds of variables, "global variables" and "local variables".

  • The first kind of MUMPS variables is called a "global" variable. These variables store information in a similar way to DISK memory in a computer. When the computer is shut down, the disk information is still saved. MUMPS global variables are persistent, and available after a MUMPS JOB is HALTed. These variables are persistent, and permanent until they are KILLed. They can be explicitly erased by the KILL command. These global variabless are process-public, and accessible from any JOB on the system at any time and can be changed by any JOB at any time even though they are actually stored on disk. These global variables are available from any subroutine, and there is only one shared copy available to every subroutine. These MUMPS global variables are not replicated in other programming languages. In other languages, information that persists over time is not as accessible as global variables, as it is stored in a database, and requires a different way of access.
  • The second kind of MUMPS variable is called a "local" variable. These variables store information in a similar way to RAM memory in a computer. When the computer is shut down, the memory is erased. MUMPS local variables are erased when a MUMPS JOB is HALTed. These variables are temporary, only existing as long as a JOB. They can be explicitly erased by the KILL command. These variables are also private to a JOB, so that only the subroutines in that JOB can view them. These local variables are available in every subroutine unless a NEWed local variable exists that hides their name. Other programming languages refer to this type of variable as "globals", because they are visible to any part of the program that is currently running. This name is inappropriate in MUMPS, because in contrast to other languages, MUMPS global variables are variables that have a higher visibility than the "global" variables used in other languages.
  • Since all MUMPS local variables are temporary and local to a JOB, there are times that a program needs to use a variable name that has already been used to refer to its own variables. The NEW command allows any already existing local variables to be hidden and a new variable name to be made available. These NEWed local variables have the same properties as other local variables, but are only visible to a particular subroutine and all subroutines that are called from it.

simple input and output

MUMPS uses the WRITE (abbreviated W) command for output and the READ (R) command for input. Unless specified by the USE (U) command, output defaults to the STDOUT equivalent for the implementation and input from STDIN equivalent.

The USE command redirects the I/O device to other devices. Its syntax varies from implementation to implementation. On older PDP-11 computers running DSM-11, devices were given integer designators. VAX-DSM and other implementations allowed strings to specify host OS devices.

The default device for any particular running instance is always $PRINCIPLE ($P) and most implementations will also accept 0 as the equivalent of $P.

	K Amount,Availablenotes,TMP
	I ($G(^ATM(1000))+$G(^ATM(500))+$G(^ATM(100)))<=0 D
	. W #,!,"There is no money available in this Machine. Sorry for the inconvenience."
	. Q	
	Set Availablenotes=$S($G(^ATM(100)):"100",1:"")
	Set Availablenotes=Availablenotes_" "_$S($G(^ATM(500)):"500",1:"")
	Set Availablenotes=Availablenotes_" "_$S($G(^ATM(1000)):"1000",1:"")
	W #,!,"Only ",Availablenotes," note(s) are available"
	R !,"Enter the Amount :",Amount:60
	I '$T D
	. W !,"Sorry! Time Out!"
	. R TMP#1:5
	. G Exit
	I Amount>10000 D
	. W !,"Please enter an amount less then 10000."
	. R TMP#1:5
	. G Withdraw
	I (Amount#100) D
	. W !,"Please enter an amount in multiples of 100."
	. R TMP#1:5
	. G Withdraw
	I (($G(^ATM(1000))*1000)+($G(^ATM(500))*500)+($G(^ATM(100))*100))<Amount D
	. W !,"Transaction failed"
	. R TMP#1:5
	. G Withdraw
	I (Amount<500)&&($G(^ATM(100))<(Amount#100)) D
	. W !,"Transaction failed"
	. R TMP#1:5
	. G Withdraw
	W !,"Thank you visit again"

; Sundara Moorthy
; Sr. Cache And MUMPS Developer

Beginning Exercises

Write a program for a Patient Information system. It should have the following options:

  1. Add new patient
  2. Edit Patient(use patient unique identification number)
  3. View Patient
    1. All
    2. Search by name

Each patient should have a unique identifier, store into the global ^PAT


MUMPS allows for self-modifying code and indirection of variables. The @ symbol is used as a prefix to indicate that the variable is actually a pointer to another variable.


> S X=1,Y="X" ; Create the initial two variables, X and Y
> W X ; Send the value of X to the current device
> W Y ; Send the value of Y to the current device
> W @Y ; Send the value of the pointed-to variable (X) to the current device

The pointer reference can contain any string that evaluates to a valid variable name, whether a LOCAL or a GLOBAL variable. The pointer reference itself can be either type of variable.

NOTE: This is not the same as code indirection, which is covered under a different article.