In this module, we will explore the fundamental data structures in MUMPS: arrays and lists. Understanding these structures is crucial for efficient data manipulation and storage in MUMPS programming.

  1. Introduction to Arrays and Lists

Arrays

  • Definition: An array in MUMPS is a collection of elements identified by indices or keys.
  • Types: MUMPS supports both single-dimensional and multi-dimensional arrays.
  • Syntax: Arrays are defined using the SET command with subscripts.

Lists

  • Definition: Lists in MUMPS are similar to arrays but are typically used to store sequences of elements.
  • Usage: Lists are often used for queue or stack operations.

  1. Creating and Using Arrays

Single-Dimensional Arrays

  • Syntax: SET array(index)=value
  • Example:
    SET fruits(1)="Apple"
    SET fruits(2)="Banana"
    SET fruits(3)="Cherry"
    

Multi-Dimensional Arrays

  • Syntax: SET array(index1,index2,...)=value
  • Example:
    SET matrix(1,1)=5
    SET matrix(1,2)=10
    SET matrix(2,1)=15
    SET matrix(2,2)=20
    

Accessing Array Elements

  • Syntax: WRITE array(index)
  • Example:
    WRITE fruits(1)  ; Output: Apple
    WRITE matrix(1,2)  ; Output: 10
    

  1. Creating and Using Lists

Defining Lists

  • Syntax: Lists are defined similarly to arrays but are often used in a sequential manner.
  • Example:
    SET list(1)="First"
    SET list(2)="Second"
    SET list(3)="Third"
    

Accessing List Elements

  • Syntax: WRITE list(index)
  • Example:
    WRITE list(2)  ; Output: Second
    

  1. Practical Examples

Example 1: Storing and Retrieving Student Grades

; Define an array to store student grades
SET grades("John")=85
SET grades("Jane")=92
SET grades("Jim")=78

; Retrieve and display grades
WRITE "John's grade: ", grades("John"), !
WRITE "Jane's grade: ", grades("Jane"), !
WRITE "Jim's grade: ", grades("Jim"), !

Example 2: Multi-Dimensional Array for a Tic-Tac-Toe Board

; Initialize a 3x3 Tic-Tac-Toe board
SET board(1,1)="X"
SET board(1,2)="O"
SET board(1,3)="X"
SET board(2,1)="O"
SET board(2,2)="X"
SET board(2,3)="O"
SET board(3,1)="X"
SET board(3,2)="O"
SET board(3,3)="X"

; Display the board
FOR i=1:1:3 DO
. FOR j=1:1:3 DO
. . WRITE board(i,j)," "
. WRITE !

  1. Exercises

Exercise 1: Create and Manipulate an Array

  1. Create an array to store the names of five cities.
  2. Retrieve and display the name of the third city.

Solution:

; Create the array
SET cities(1)="New York"
SET cities(2)="Los Angeles"
SET cities(3)="Chicago"
SET cities(4)="Houston"
SET cities(5)="Phoenix"

; Retrieve and display the third city
WRITE "The third city is: ", cities(3), !

Exercise 2: Multi-Dimensional Array for a Calendar

  1. Create a 2D array to represent a calendar for a week (7 days) with 3 time slots each day.
  2. Assign values to the array to represent events.
  3. Retrieve and display the event for the second day, second time slot.

Solution:

; Create the 2D array
SET calendar(1,1)="Meeting"
SET calendar(1,2)="Lunch"
SET calendar(1,3)="Workout"
SET calendar(2,1)="Conference"
SET calendar(2,2)="Project Work"
SET calendar(2,3)="Dinner"
; ... (continue for the rest of the week)

; Retrieve and display the event for the second day, second time slot
WRITE "Event on the second day, second time slot: ", calendar(2,2), !

  1. Common Mistakes and Tips

Common Mistakes

  • Incorrect Indexing: Ensure that indices are correctly specified, especially in multi-dimensional arrays.
  • Uninitialized Elements: Accessing elements that have not been initialized can lead to unexpected results.

Tips

  • Consistent Indexing: Use consistent and meaningful indices to make your code more readable.
  • Initialization: Always initialize your arrays and lists before accessing them.

Conclusion

In this module, we covered the basics of arrays and lists in MUMPS, including their creation, manipulation, and practical usage. Understanding these data structures is essential for efficient data handling in MUMPS programming. In the next module, we will delve into file handling in MUMPS, building on the concepts learned here.

© Copyright 2024. All rights reserved