COBOL数组应用求助:遍历预订类型时重复输出客户信息问题
Hey there! Let's work through the two issues you're hitting with your COBOL booking array program. I’ve dealt with similar array and grouping challenges before, so here’s practical advice for each problem:
1. Simplifying Booking Type Traversal
Manual array index management can get clunky—COBOL’s PERFORM VARYING statement is made for this exact scenario. It handles the loop counter, boundary checks, and incrementing automatically, making your code cleaner and less error-prone.
For example, if you have an array of booking entries defined in your Data Division like:
01 BOOKING-ARRAY. 05 BOOKING-ENTRY OCCURS 100 TIMES INDEXED BY WS-BOOK-IDX. 10 CLIENT-NO PIC 9(5). 10 BOOKING-TYPE PIC X(20). 10 TRIP-COST PIC 9(7)V99. 10 CLIENT-NAME PIC X(30). 10 CLIENT-ADDRESS PIC X(50).
You can traverse it smoothly with:
PERFORM VARYING WS-BOOK-IDX FROM 1 BY 1 UNTIL WS-BOOK-IDX > 100 * Process each booking entry here: * e.g., access CLIENT-NO(WS-BOOK-IDX), TRIP-COST(WS-BOOK-IDX) END-PERFORM
Since your data is sorted by client number, you can even add a condition to exit the inner loop early when the client ID changes, optimizing the traversal further.
2. Fixing Duplicate Client Information Display
The duplicate client details happen because you’re printing the client’s ID, name, and address every time you process one of their bookings. Since your data is sorted by client number, you can track the previous client to only print header info when a new client starts.
Here’s how to implement this:
First, add working storage variables to track the previous client and accumulate trip costs for averaging:
01 WS-WORK-AREAS. 05 WS-PREV-CLIENT-NO PIC 9(5) VALUE ZEROS. 05 WS-TOTAL-TRIP-COST PIC 9(9)V99 VALUE ZEROS. 05 WS-BOOKING-COUNT PIC 9(3) VALUE ZEROS. 05 WS-AVERAGE-COST PIC 9(7)V99.
Then, in your processing logic:
PERFORM VARYING WS-BOOK-IDX FROM 1 BY 1 UNTIL WS-BOOK-IDX > 100 * Check if we're dealing with a new client IF CLIENT-NO(WS-BOOK-IDX) NOT EQUAL TO WS-PREV-CLIENT-NO * Print the previous client's average if we're not on the first client IF WS-PREV-CLIENT-NO NOT EQUAL TO ZEROS COMPUTE WS-AVERAGE-COST = WS-TOTAL-TRIP-COST / WS-BOOKING-COUNT DISPLAY "=== Average Trip Cost for Client " WS-PREV-CLIENT-NO ": " WS-AVERAGE-COST " ===" END-IF * Print the new client's header info ONCE DISPLAY NEWLINE DISPLAY "Client No: " CLIENT-NO(WS-BOOK-IDX) DISPLAY "Name: " CLIENT-NAME(WS-BOOK-IDX) DISPLAY "Address: " CLIENT-ADDRESS(WS-BOOK-IDX) * Reset accumulators for the new client MOVE CLIENT-NO(WS-BOOK-IDX) TO WS-PREV-CLIENT-NO MOVE ZEROS TO WS-TOTAL-TRIP-COST WS-BOOKING-COUNT END-IF * Accumulate current booking data for averaging ADD TRIP-COST(WS-BOOK-IDX) TO WS-TOTAL-TRIP-COST ADD 1 TO WS-BOOKING-COUNT * Optional: Print individual booking details here if needed DISPLAY "- Booking Type: " BOOKING-TYPE(WS-BOOK-IDX) " | Cost: " TRIP-COST(WS-BOOK-IDX) END-PERFORM * Don't forget to print the last client's average after the loop ends IF WS-PREV-CLIENT-NO NOT EQUAL TO ZEROS COMPUTE WS-AVERAGE-COST = WS-TOTAL-TRIP-COST / WS-BOOKING-COUNT DISPLAY NEWLINE DISPLAY "=== Average Trip Cost for Client " WS-PREV-CLIENT-NO ": " WS-AVERAGE-COST " ===" END-IF
This way, client headers only print once per unique client, and you correctly calculate and display the average trip cost for each group.
If you can share more of your Data Division definitions or full processing logic, I can tweak this advice to match your exact setup—but these steps should resolve both of your current issues!
内容的提问来源于stack exchange,提问作者GreenSaucer42

