Operators in MUMPS
Learn arithmetic, string, comparison, logical, and pattern matching operators in MUMPS, including its unique strict left-to-right evaluation rule
Operators in MUMPS are heavily influenced by the language’s typeless nature: the same value can be treated as a string or a number, and operators decide which interpretation to apply. This leads to some operators (like _ for concatenation and [ for substring containment) that look unusual to programmers coming from C-family languages.
MUMPS also makes a famously unusual choice about how expressions are evaluated. Unlike almost every other language, MUMPS has no operator precedence. Expressions are evaluated strictly left-to-right, and parentheses are the only way to force a different order. 2+3*4 in MUMPS evaluates to 20, not 14.
This tutorial walks through the full operator set: arithmetic, string, comparison, logical, and the pattern match operator that is uniquely MUMPS.
Arithmetic Operators
MUMPS supports the standard arithmetic operators plus a few that are less common in other languages: integer division (\), modulo (#), and exponentiation (**).
Create a file named operators.m:
operators ; Operators in MUMPS
; --- Arithmetic operators ---
set a=10,b=4
write "a + b = ",a+b,!
write "a - b = ",a-b,!
write "a * b = ",a*b,!
write "a / b = ",a/b,!
write "a \ b = ",a\b,!
write "a # b = ",a#b,!
write "a ** b = ",a**b,!
write "-a = ",-a,!
;
; --- Typeless numeric coercion ---
; A string is converted to a number by taking its leading numeric prefix.
set s="42abc"
write "s + 8 = ",s+8,!
write "s * 2 = ",s*2,!
;
; --- String concatenation with _ ---
set first="Hello",second="World"
write first_", "_second_"!",!
;
; --- Comparison operators ---
; ' is the NOT prefix: '= means not equal, '> means not greater than.
write "5 = 5 : ",5=5,!
write "5 '= 6 : ",5'=6,!
write "5 > 3 : ",5>3,!
write "5 < 3 : ",5<3,!
write "5 '> 3 : ",5'>3,!
;
; --- Logical operators ---
; & is AND, ! is OR, ' is NOT. Any nonzero number is true.
write "1 & 0 : ",1&0,!
write "1 ! 0 : ",1!0,!
write "'1 : ",'1,!
write "'0 : ",'0,!
;
; --- String operators ---
; [ tests substring containment; ] tests collation order.
write "hello [ ell : ","hello"["ell",!
write "abd ] abc : ","abd"]"abc",!
;
; --- Pattern match operator ? ---
; 3A3N means exactly 3 alphabetic followed by exactly 3 numeric chars.
write "abc123 ? 3A3N : ","abc123"?3A3N,!
write "ab12 ? 3A3N : ","ab12"?3A3N,!
;
; --- Left-to-right evaluation (no precedence) ---
write "2+3*4 = ",2+3*4,!
write "2+(3*4) = ",2+(3*4),!
quit
A Tour of the Less Familiar Operators
String concatenation with _
In most languages _ is a valid identifier character. In MUMPS it’s the concatenation operator. "Hello"_", "_"World" produces "Hello, World". There is no + overloading for strings — if you write "Hello"+5 MUMPS will coerce "Hello" to the number 0 and give you 5.
Substring containment with [
A[B is true when B is a substring of A. Read it as “A contains B”. So "hello"["ell" is 1 (true), and "hello"["xyz" is 0 (false).
Collation order with ]
A]B is true when A sorts after B in MUMPS’s collation order. "abd"]"abc" is 1. There is also a ]] operator (sorts after in subscript collation order, where numbers sort before strings).
Pattern match with ?
The ? operator matches a string against a pattern built from character class codes and counts: A for alpha, N for numeric, P for punctuation, C for control, E for any. "abc123"?3A3N is true because the string is exactly three letters followed by exactly three digits. Variable counts use . — for example 1A.N means one letter followed by zero or more digits.
No precedence
Every binary operator in MUMPS has the same precedence and associates left-to-right. This was a deliberate simplification in the 1960s implementation — there’s no precedence table to memorize, no surprises about whether && binds tighter than ||. The cost is that you must parenthesize anything that depends on conventional arithmetic order:
write 2+3*4,! ; → 20 ((2+3)*4)
write 2+(3*4),! ; → 14
write 10-2-3,! ; → 5 ((10-2)-3, the conventional answer)
Running with Docker
| |
Expected Output
a + b = 14
a - b = 6
a * b = 40
a / b = 2.5
a \ b = 2
a # b = 2
a ** b = 10000
-a = -10
s + 8 = 50
s * 2 = 84
Hello, World!
5 = 5 : 1
5 '= 6 : 1
5 > 3 : 1
5 < 3 : 0
5 '> 3 : 0
1 & 0 : 0
1 ! 0 : 1
'1 : 0
'0 : 1
hello [ ell : 1
abd ] abc : 1
abc123 ? 3A3N : 1
ab12 ? 3A3N : 0
2+3*4 = 20
2+(3*4) = 14
Boolean results in MUMPS are simply the integers 1 and 0; there is no separate boolean type.
Key Concepts
- No operator precedence: expressions evaluate strictly left-to-right. Use parentheses whenever you want conventional arithmetic order.
_concatenates strings, not+. The+operator always forces numeric interpretation.\is integer division and#is modulo. Regular/produces a fractional result even for integer operands.'is the NOT prefix, used to build operators like'=(not equal) and'>(not greater than). There is no!=token.&is AND,!is OR as binary operators. Inside awriteargument list, a bare!instead means “output a newline” — context determines which.[tests substring containment and]tests collation order — operators that don’t exist as built-ins in most languages.- The
?pattern match operator is built into the language itself, not a library function. Patterns use single-letter class codes (A,N,P,C,E) with repetition counts. - Typeless coercion: when a string is used in arithmetic, MUMPS uses its leading numeric prefix;
"42abc"+8is50, and a string with no numeric prefix coerces to0.
Running Today
All examples can be run using Docker:
docker pull yottadb/yottadb-base:latest-master
Comments
Loading comments...
Leave a Comment