In this section, we will explore how to store and retrieve data in MUMPS. This is a fundamental skill for any MUMPS programmer, as it allows you to manage and manipulate data effectively.

Key Concepts

  1. Global Variables: Persistent storage that can be accessed across different sessions.
  2. Local Variables: Temporary storage that exists only within the current session or scope.
  3. Data Storage: Techniques for saving data in global variables.
  4. Data Retrieval: Techniques for accessing and manipulating stored data.

Global Variables

Global variables in MUMPS are used for persistent storage. They are stored in the database and can be accessed by any process.

Syntax

Global variables are denoted by a caret (^) followed by the variable name.

^globalName

Example

SET ^patient(1, "name") = "John Doe"
SET ^patient(1, "age") = 30

In this example, we are storing the name and age of a patient in a global variable named ^patient.

Local Variables

Local variables are used for temporary storage and are only accessible within the current session or scope.

Syntax

Local variables do not have a caret (^) and are simply denoted by their name.

SET localName = "Jane Doe"
SET localAge = 25

Storing Data

To store data in MUMPS, you use the SET command. This command assigns a value to a variable.

Example

SET ^employee(1, "name") = "Alice Smith"
SET ^employee(1, "position") = "Manager"
SET ^employee(1, "salary") = 75000

In this example, we are storing information about an employee in a global variable named ^employee.

Retrieving Data

To retrieve data in MUMPS, you simply reference the variable name.

Example

WRITE ^employee(1, "name"), !
WRITE ^employee(1, "position"), !
WRITE ^employee(1, "salary"), !

This will output:

Alice Smith
Manager
75000

Practical Example

Let's combine storing and retrieving data in a practical example.

Code

; Store data
SET ^student(1, "name") = "Bob Johnson"
SET ^student(1, "grade") = "A"
SET ^student(2, "name") = "Emily Davis"
SET ^student(2, "grade") = "B"

; Retrieve and display data
WRITE "Student 1: ", ^student(1, "name"), " - Grade: ", ^student(1, "grade"), !
WRITE "Student 2: ", ^student(2, "name"), " - Grade: ", ^student(2, "grade"), !

Explanation

  1. Storing Data: We store the names and grades of two students in the global variable ^student.
  2. Retrieving Data: We retrieve and display the stored data using the WRITE command.

Exercises

Exercise 1

Task: Store the following information about two books in a global variable named ^book:

  • Book 1: Title: "MUMPS Programming", Author: "John Smith", Year: 2020
  • Book 2: Title: "Advanced MUMPS", Author: "Jane Doe", Year: 2021

Solution:

; Store data
SET ^book(1, "title") = "MUMPS Programming"
SET ^book(1, "author") = "John Smith"
SET ^book(1, "year") = 2020
SET ^book(2, "title") = "Advanced MUMPS"
SET ^book(2, "author") = "Jane Doe"
SET ^book(2, "year") = 2021

; Retrieve and display data
WRITE "Book 1: ", ^book(1, "title"), " by ", ^book(1, "author"), " (", ^book(1, "year"), ")", !
WRITE "Book 2: ", ^book(2, "title"), " by ", ^book(2, "author"), " (", ^book(2, "year"), ")", !

Exercise 2

Task: Retrieve and display the information stored in the ^book global variable from Exercise 1.

Solution:

; Retrieve and display data
WRITE "Book 1: ", ^book(1, "title"), " by ", ^book(1, "author"), " (", ^book(1, "year"), ")", !
WRITE "Book 2: ", ^book(2, "title"), " by ", ^book(2, "author"), " (", ^book(2, "year"), ")", !

Common Mistakes and Tips

  1. Forgetting the Caret (^): Always remember to use the caret when working with global variables.
  2. Incorrect Indexing: Ensure that you use the correct indices when storing and retrieving data.
  3. Data Types: MUMPS treats all data as strings, so be mindful of this when performing operations.

Conclusion

In this section, we covered the basics of storing and retrieving data in MUMPS. We learned about global and local variables, how to store data using the SET command, and how to retrieve data using the WRITE command. We also provided practical examples and exercises to reinforce these concepts. In the next section, we will delve into more complex data structures such as arrays and lists.

© Copyright 2024. All rights reserved