What’s DCL-DS?
DCL-DS lets you define a data structure in free-format RPGLE a neat container for grouping related fields, like toppings on a pizza. Think of it as a pizza box that holds all your order details together.
RPG uses three instructions to declare data structures. The syntax is as simple as a margherita:
Dcl-subf (Declare subfield)
End-ds (End data structure)
The dcl-subf is optional as all fields within the data structure are obviously declared - so most often it will look like this:
field-name1 data-type;
field-name2 data-type;
END-DS;
Fields can be numbers, strings, indicators, or more, and you access them with structure.field notation. It’s like telling the pizza chef exactly what goes on your pie.
Using DCL-DS in IBM i RPGLE Programming
// Purpose: Teach data structure definitions with a pizza-ordering twist
// Author: Nick Litten (the RPGLE pizza architect)
// Declare constants - because pizza rules don’t change
DCL-C PIZZA_PRICE 12.99; // Cost of a large pizza
DCL-C DELIVERY_FEE 3.50; // Fee for pizza delivery
DCL-C MAX_TOPPINGS 5; // Max toppings before pizza chaos
// Declare data structure for a pizza order
DCL-DS Order qualified;
PizzaQty PACKED(3:0); // Number of pizzas
ToppingCount INT(5); // Number of extra toppings
CustomerName CHAR(30); // Customer’s name
TotalCost PACKED(7:2); // Total order cost
IsDelivery IND; // Delivery or pickup?
END-DS;
// Declare variables - for building our order summary
DCL-S OrderMsg CHAR(100); // Message to display order details
// Main procedure - let’s toss some code in the oven!
// Initialize the order data structure
Order.PizzaQty = 2; // Two pizzas, because sharing is caring
Order.ToppingCount = 4; // Extra cheese, peppers, and more
Order.CustomerName = 'CodeMaster Joe'; // Our loyal coder customer
Order.IsDelivery = *ON; // Deliver to the coding cave
// Calculate total cost: pizzas + toppings + delivery (if applicable)
Order.TotalCost = (Order.PizzaQty * PIZZA_PRICE) +
(Order.ToppingCount * 1.50);
IF Order.IsDelivery;
Order.TotalCost += DELIVERY_FEE;
ENDIF;
// Build order summary, with a topping overload check
IF Order.ToppingCount > MAX_TOPPINGS;
OrderMsg = 'Hey ' + %TRIM(Order.CustomerName) +
', ' + %CHAR(Order.ToppingCount) +
' toppings? Pizza might need a crane!';
ELSE;
OrderMsg = 'Order for ' + %TRIM(Order.CustomerName) +
%CHAR(Order.ToppingCount) + ' toppings, Total: $' +
%CHAR(Order.TotalCost);
ENDIF;
// Display the order summary
DSPLY OrderMsg;
// Shut down the pizza shop when done
The Code Breakdown
This program manages a pizza order using a data structure. Here’s the recipe:
Constants
- PIZZA_PRICE: $12.99 for a large pizza.
- DELIVERY_FEE: $3.50 for delivery to your coding desk.
- MAX_TOPPINGS: 5, to prevent a topping avalanche.
Data Structure
Order (DCL-DS)
- PizzaQty: Number of pizzas ordered.
- ToppingCount: Number of extra toppings.
- CustomerName: The customer’s name (because coders have cool names).
- TotalCost: The final bill.
- IsDelivery: An indicator to check if it’s delivery or pickup.
Variables
- OrderMsg: A string to display the order summary.
Logic
- Initializes the Order data structure with sample data (2 pizzas, 4 toppings, customer CodeMaster Joe, delivery).
- Calculates the total cost: (PizzaQty * PIZZA_PRICE) + (ToppingCount * 1.50) + DELIVERY_FEE (if delivery).
- Checks if ToppingCount exceeds MAX_TOPPINGS. If so, it warns about pizza chaos; otherwise, it builds a friendly order summary.
- Uses DSPLY to show the order details, because console output is the coder’s applause.
Why DCL-DS Rocks
Data structures keep your code tidy, like a well-organized pizza kitchen. Instead of juggling separate variables like a clumsy chef, DCL-DS groups related data, making it easier to read and maintain. Plus, it’s perfect for passing data to subprocedures or files.
Think of it as handing off a complete pizza order in one box.
How to Run
- Drop this code into your IBM i system.
- Compile it with CRTRPGMOD or CRTBNDRPG.
- Run it and watch the DSPLY output serve up your pizza order.
Hints and Tips for coding DCL-DS Declare Data Structure
Name your data structure and fields clearly, like Order.PizzaQty instead of DS1.QTY.
Avoid using special characters like @ or # in names
Vague names are like ordering “stuff” on your pizza; nobody’s happy, especially future you debugging at 2 a.m.
So, fire up your IBM i, toss this code in the oven, and enjoy a perfectly structured RPGLE program with DCL-DS. May your data structures be as solid as a deep-dish crust!
