In this section, we will explore how to handle input and output (I/O) operations in REXX. Understanding I/O is crucial for interacting with users and other systems, making your programs more dynamic and useful.
Key Concepts
- Reading Input from the User
- Displaying Output to the User
- File I/O Basics
- Reading Input from the User
REXX provides a straightforward way to read input from the user using the PULL
instruction.
Example: Reading User Input
/* REXX program to read user input */ say "Please enter your name:" pull name say "Hello, " name "!"
Explanation:
say "Please enter your name:"
displays a prompt to the user.pull name
reads the input from the user and stores it in the variablename
.say "Hello, " name "!"
outputs a greeting message using the input.
Exercise: Reading and Displaying User Input
Task: Write a REXX program that asks the user for their age and then displays a message indicating how old they will be in 5 years.
Solution:
/* REXX program to calculate age in 5 years */ say "Please enter your age:" pull age future_age = age + 5 say "In 5 years, you will be " future_age " years old."
- Displaying Output to the User
The SAY
instruction is used to display output to the user. It can be used to print strings, variables, and expressions.
Example: Displaying Output
Explanation:
name = "Alice"
assigns the string "Alice" to the variablename
.age = 30
assigns the number 30 to the variableage
.say "Name: " name
andsay "Age: " age
display the values ofname
andage
.
Exercise: Displaying Calculated Output
Task: Write a REXX program that calculates the sum of two numbers and displays the result.
Solution:
/* REXX program to calculate and display the sum of two numbers */ num1 = 10 num2 = 20 sum = num1 + num2 say "The sum of " num1 " and " num2 " is " sum "."
- File I/O Basics
REXX allows you to read from and write to files, which is essential for handling larger datasets and persistent storage.
Writing to a File
To write to a file, you use the CALL LINEOUT
instruction.
Example: Writing to a File
/* REXX program to write to a file */ filename = "output.txt" call lineout filename, "This is a line of text." call lineout filename, "This is another line of text."
Explanation:
filename = "output.txt"
specifies the name of the file.call lineout filename, "This is a line of text."
writes a line of text to the file.- Each
call lineout
appends a new line to the file.
Reading from a File
To read from a file, you use the CALL LINEIN
instruction.
Example: Reading from a File
/* REXX program to read from a file */ filename = "output.txt" line = linein(filename) do while line \= "" say line line = linein(filename) end
Explanation:
filename = "output.txt"
specifies the name of the file.line = linein(filename)
reads the first line from the file.- The
do while
loop continues to read and display lines until the end of the file is reached.
Exercise: File I/O
Task: Write a REXX program that writes three lines of text to a file and then reads and displays the content of the file.
Solution:
/* REXX program to write and read from a file */ filename = "example.txt" call lineout filename, "First line of text." call lineout filename, "Second line of text." call lineout filename, "Third line of text." /* Reading from the file */ line = linein(filename) do while line \= "" say line line = linein(filename) end
Summary
In this section, we covered the basics of input and output in REXX:
- Reading user input using
PULL
. - Displaying output using
SAY
. - Writing to and reading from files using
CALL LINEOUT
andCALL LINEIN
.
Understanding these concepts is essential for creating interactive and data-driven REXX programs. In the next module, we will delve into more complex programming concepts, such as functions and subroutines.
REXX Programming Course
Module 1: Introduction to REXX
- What is REXX?
- Setting Up the REXX Environment
- Hello World in REXX
- Basic Syntax and Structure
- Variables and Data Types
Module 2: Basic Programming Concepts
- Operators and Expressions
- Control Structures: IF/THEN/ELSE
- Loops: DO and LEAVE
- Input and Output
- Basic String Manipulation
Module 3: Intermediate REXX Programming
Module 4: Advanced REXX Programming
- Advanced String Manipulation
- Parsing Techniques
- Interfacing with External Programs
- REXX Macros
- Performance Optimization