MainframeMaster

COBOL Tutorial

COBOL LEADING

The LEADING keyword represents specialized string processing capabilities within COBOL INSPECT statement environments, providing sophisticated leading character identification and manipulation mechanisms that enable precise string trimming, character counting, and replacement operations focused specifically on consecutive characters appearing at the beginning of target strings. This keyword embodies advanced text processing principles by supporting automated leading character detection, enabling sophisticated string cleanup operations, and facilitating comprehensive data standardization patterns while maintaining string integrity, ensuring precise character processing, and supporting complex text manipulation requirements across enterprise applications requiring detailed string formatting, automated data cleanup, and reliable leading character handling with optimized performance characteristics and consistent text processing standards.

LEADING with INSPECT

Leading Character Processing
cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
*> Basic LEADING usage with INSPECT INSPECT string-variable TALLYING counter FOR LEADING character INSPECT string-variable REPLACING LEADING character BY replacement *> Examples: 01 WS-TEXT-FIELD PIC X(20) VALUE " HELLO WORLD". 01 WS-SPACE-COUNT PIC 9(2) VALUE 0. *> Count leading spaces INSPECT WS-TEXT-FIELD TALLYING WS-SPACE-COUNT FOR LEADING SPACES *> Result: WS-SPACE-COUNT = 3 *> Remove leading spaces INSPECT WS-TEXT-FIELD REPLACING LEADING SPACES BY " " *> Result: WS-TEXT-FIELD = " HELLO WORLD " *> Remove leading zeros from numeric display 01 WS-NUMERIC-DISPLAY PIC X(10) VALUE "0001234567". INSPECT WS-NUMERIC-DISPLAY REPLACING LEADING "0" BY SPACES *> Result: WS-NUMERIC-DISPLAY = " 1234567" *> Multiple character leading processing 01 WS-DATA-FIELD PIC X(15) VALUE "XXXImportantData". INSPECT WS-DATA-FIELD REPLACING LEADING "X" BY SPACES *> Result: WS-DATA-FIELD = " ImportantData"
String Processing
Character Manipulation
INSPECT

Comprehensive LEADING Examples

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
IDENTIFICATION DIVISION. PROGRAM-ID. LEADING-PROCESSING-DEMO. DATA DIVISION. WORKING-STORAGE SECTION. *> Test data for LEADING operations 01 WS-TEST-STRINGS. 05 WS-STRING-1 PIC X(30) VALUE " HELLO WORLD ". 05 WS-STRING-2 PIC X(25) VALUE "000012345". 05 WS-STRING-3 PIC X(20) VALUE "AAAABBBCCCAAA". 05 WS-STRING-4 PIC X(15) VALUE "!!!URGENT!!!". 05 WS-STRING-5 PIC X(30) VALUE "<<>>". *> Backup copies for comparison 01 WS-ORIGINAL-STRINGS. 05 WS-ORIG-STRING-1 PIC X(30). 05 WS-ORIG-STRING-2 PIC X(25). 05 WS-ORIG-STRING-3 PIC X(20). 05 WS-ORIG-STRING-4 PIC X(15). 05 WS-ORIG-STRING-5 PIC X(30). *> Counters for TALLYING operations 01 WS-COUNTERS. 05 WS-SPACE-COUNT PIC 9(3) VALUE 0. 05 WS-ZERO-COUNT PIC 9(3) VALUE 0. 05 WS-CHAR-COUNT PIC 9(3) VALUE 0. 05 WS-SYMBOL-COUNT PIC 9(3) VALUE 0. *> Customer data processing examples 01 WS-CUSTOMER-DATA. 05 WS-CUSTOMER-ID PIC X(12) VALUE "000000123456". 05 WS-CUSTOMER-NAME PIC X(30) VALUE " JOHN SMITH". 05 WS-PHONE-NUMBER PIC X(15) VALUE "000-555-1234". 05 WS-ACCOUNT-CODE PIC X(10) VALUE "AAAACCT001". *> Financial data examples 01 WS-FINANCIAL-DATA. 05 WS-AMOUNT-DISPLAY PIC X(15) VALUE "00000012.95". 05 WS-TRANSACTION-ID PIC X(20) VALUE "TXN00000000012345". 05 WS-REFERENCE-NUM PIC X(18) VALUE "REF000000000000456". *> Report formatting variables 01 WS-REPORT-FIELDS. 05 WS-HEADER-LINE PIC X(60) VALUE "***LEADING PROCESSING REPORT***". 05 WS-DETAIL-LINE PIC X(60). 05 WS-PADDED-FIELD PIC X(40) VALUE ";;;;;;;;Data with leading chars". PROCEDURE DIVISION. MAIN-PROCESSING. DISPLAY "=== COBOL LEADING PROCESSING DEMONSTRATION ===" DISPLAY SPACES PERFORM SAVE-ORIGINAL-STRINGS PERFORM DEMONSTRATE-LEADING-TALLYING PERFORM DEMONSTRATE-LEADING-REPLACEMENT PERFORM CUSTOMER-DATA-CLEANUP PERFORM FINANCIAL-DATA-FORMATTING PERFORM ADVANCED-LEADING-SCENARIOS DISPLAY "=== LEADING PROCESSING COMPLETE ===" STOP RUN. SAVE-ORIGINAL-STRINGS. MOVE WS-STRING-1 TO WS-ORIG-STRING-1 MOVE WS-STRING-2 TO WS-ORIG-STRING-2 MOVE WS-STRING-3 TO WS-ORIG-STRING-3 MOVE WS-STRING-4 TO WS-ORIG-STRING-4 MOVE WS-STRING-5 TO WS-ORIG-STRING-5. DEMONSTRATE-LEADING-TALLYING. DISPLAY "=== LEADING TALLYING OPERATIONS ===" DISPLAY SPACES *> Count leading spaces MOVE 0 TO WS-SPACE-COUNT INSPECT WS-STRING-1 TALLYING WS-SPACE-COUNT FOR LEADING SPACES DISPLAY "Original string: '" WS-ORIG-STRING-1 "'" DISPLAY "Leading spaces counted: " WS-SPACE-COUNT DISPLAY SPACES *> Count leading zeros MOVE 0 TO WS-ZERO-COUNT INSPECT WS-STRING-2 TALLYING WS-ZERO-COUNT FOR LEADING "0" DISPLAY "Original string: '" WS-ORIG-STRING-2 "'" DISPLAY "Leading zeros counted: " WS-ZERO-COUNT DISPLAY SPACES *> Count leading 'A' characters MOVE 0 TO WS-CHAR-COUNT INSPECT WS-STRING-3 TALLYING WS-CHAR-COUNT FOR LEADING "A" DISPLAY "Original string: '" WS-ORIG-STRING-3 "'" DISPLAY "Leading 'A' characters counted: " WS-CHAR-COUNT DISPLAY SPACES *> Count leading exclamation marks MOVE 0 TO WS-SYMBOL-COUNT INSPECT WS-STRING-4 TALLYING WS-SYMBOL-COUNT FOR LEADING "!" DISPLAY "Original string: '" WS-ORIG-STRING-4 "'" DISPLAY "Leading '!' symbols counted: " WS-SYMBOL-COUNT DISPLAY SPACES. DEMONSTRATE-LEADING-REPLACEMENT. DISPLAY "=== LEADING REPLACEMENT OPERATIONS ===" DISPLAY SPACES *> Replace leading spaces with underscores DISPLAY "Before space replacement: '" WS-STRING-1 "'" INSPECT WS-STRING-1 REPLACING LEADING SPACES BY "_" DISPLAY "After space replacement: '" WS-STRING-1 "'" DISPLAY SPACES *> Replace leading zeros with spaces DISPLAY "Before zero replacement: '" WS-STRING-2 "'" INSPECT WS-STRING-2 REPLACING LEADING "0" BY " " DISPLAY "After zero replacement: '" WS-STRING-2 "'" DISPLAY SPACES *> Replace leading 'A' with 'X' DISPLAY "Before 'A' replacement: '" WS-STRING-3 "'" INSPECT WS-STRING-3 REPLACING LEADING "A" BY "X" DISPLAY "After 'A' replacement: '" WS-STRING-3 "'" DISPLAY SPACES *> Replace leading exclamation marks with spaces DISPLAY "Before '!' replacement: '" WS-STRING-4 "'" INSPECT WS-STRING-4 REPLACING LEADING "!" BY " " DISPLAY "After '!' replacement: '" WS-STRING-4 "'" DISPLAY SPACES. CUSTOMER-DATA-CLEANUP. DISPLAY "=== CUSTOMER DATA CLEANUP WITH LEADING ===" DISPLAY SPACES *> Clean up customer ID - remove leading zeros DISPLAY "Customer ID cleanup:" DISPLAY " Original: " WS-CUSTOMER-ID INSPECT WS-CUSTOMER-ID REPLACING LEADING "0" BY " " DISPLAY " Cleaned: " WS-CUSTOMER-ID DISPLAY SPACES *> Clean up customer name - remove leading spaces DISPLAY "Customer name cleanup:" DISPLAY " Original: '" WS-CUSTOMER-NAME "'" INSPECT WS-CUSTOMER-NAME REPLACING LEADING SPACES BY "." DISPLAY " Cleaned: '" WS-CUSTOMER-NAME "'" DISPLAY SPACES *> Clean up account code - remove leading 'A' characters DISPLAY "Account code cleanup:" DISPLAY " Original: " WS-ACCOUNT-CODE INSPECT WS-ACCOUNT-CODE REPLACING LEADING "A" BY " " DISPLAY " Cleaned: " WS-ACCOUNT-CODE DISPLAY SPACES. FINANCIAL-DATA-FORMATTING. DISPLAY "=== FINANCIAL DATA FORMATTING ===" DISPLAY SPACES *> Format amount display - remove leading zeros DISPLAY "Amount formatting:" DISPLAY " Original amount: " WS-AMOUNT-DISPLAY INSPECT WS-AMOUNT-DISPLAY REPLACING LEADING "0" BY " " DISPLAY " Formatted amount:" WS-AMOUNT-DISPLAY DISPLAY SPACES *> Format transaction ID - clean leading chars DISPLAY "Transaction ID formatting:" DISPLAY " Original: " WS-TRANSACTION-ID *> Count leading zeros in transaction ID MOVE 0 TO WS-ZERO-COUNT INSPECT WS-TRANSACTION-ID TALLYING WS-ZERO-COUNT FOR LEADING "0" AFTER INITIAL "TXN" DISPLAY " Leading zeros after 'TXN': " WS-ZERO-COUNT DISPLAY SPACES *> Format reference number DISPLAY "Reference number formatting:" DISPLAY " Original: " WS-REFERENCE-NUM INSPECT WS-REFERENCE-NUM REPLACING LEADING "0" BY " " AFTER INITIAL "REF" DISPLAY " Formatted:" WS-REFERENCE-NUM DISPLAY SPACES. ADVANCED-LEADING-SCENARIOS. DISPLAY "=== ADVANCED LEADING SCENARIOS ===" DISPLAY SPACES PERFORM LEADING-WITH-MULTIPLE-CHARS PERFORM LEADING-IN-LOOPS PERFORM LEADING-WITH-CONDITIONS PERFORM REPORT-FORMATTING-WITH-LEADING. LEADING-WITH-MULTIPLE-CHARS. 01 WS-MULTI-CHAR-STRING PIC X(30) VALUE "ABCABCABCDEFGHIJ". 01 WS-PATTERN-COUNT PIC 9(2) VALUE 0. DISPLAY "Multiple character LEADING processing:" DISPLAY " Original string: " WS-MULTI-CHAR-STRING *> Count leading "ABC" patterns INSPECT WS-MULTI-CHAR-STRING TALLYING WS-PATTERN-COUNT FOR LEADING "ABC" DISPLAY " Leading 'ABC' patterns: " WS-PATTERN-COUNT *> Replace leading "ABC" with "XYZ" INSPECT WS-MULTI-CHAR-STRING REPLACING LEADING "ABC" BY "XYZ" DISPLAY " After replacement: " WS-MULTI-CHAR-STRING DISPLAY SPACES. LEADING-IN-LOOPS. 01 WS-ARRAY-DATA. 05 WS-DATA-ITEMS OCCURS 5 TIMES PIC X(15). 01 WS-LOOP-INDEX PIC 9(2). 01 WS-TEMP-COUNT PIC 9(2). DISPLAY "LEADING processing in loops:" *> Initialize array with test data MOVE "000ABC123" TO WS-DATA-ITEMS(1) MOVE "00DEF456" TO WS-DATA-ITEMS(2) MOVE "0GHI789" TO WS-DATA-ITEMS(3) MOVE "JKL012" TO WS-DATA-ITEMS(4) MOVE "000000MNO" TO WS-DATA-ITEMS(5) PERFORM VARYING WS-LOOP-INDEX FROM 1 BY 1 UNTIL WS-LOOP-INDEX > 5 DISPLAY " Item " WS-LOOP-INDEX ":" DISPLAY " Before: " WS-DATA-ITEMS(WS-LOOP-INDEX) *> Count leading zeros MOVE 0 TO WS-TEMP-COUNT INSPECT WS-DATA-ITEMS(WS-LOOP-INDEX) TALLYING WS-TEMP-COUNT FOR LEADING "0" DISPLAY " Leading zeros: " WS-TEMP-COUNT *> Remove leading zeros INSPECT WS-DATA-ITEMS(WS-LOOP-INDEX) REPLACING LEADING "0" BY " " DISPLAY " After: " WS-DATA-ITEMS(WS-LOOP-INDEX) DISPLAY SPACES END-PERFORM. LEADING-WITH-CONDITIONS. 01 WS-CONDITIONAL-DATA PIC X(20) VALUE "***IMPORTANT DATA". 01 WS-MARKER-COUNT PIC 9(2) VALUE 0. DISPLAY "Conditional LEADING processing:" DISPLAY " Original data: " WS-CONDITIONAL-DATA *> Count leading asterisks INSPECT WS-CONDITIONAL-DATA TALLYING WS-MARKER-COUNT FOR LEADING "*" DISPLAY " Leading asterisks: " WS-MARKER-COUNT *> Conditional processing based on count IF WS-MARKER-COUNT > 2 DISPLAY " High priority data detected" INSPECT WS-CONDITIONAL-DATA REPLACING LEADING "*" BY "!" DISPLAY " Marked as urgent: " WS-CONDITIONAL-DATA ELSE DISPLAY " Normal priority data" INSPECT WS-CONDITIONAL-DATA REPLACING LEADING "*" BY " " DISPLAY " Cleaned data: " WS-CONDITIONAL-DATA END-IF DISPLAY SPACES. REPORT-FORMATTING-WITH-LEADING. 01 WS-REPORT-AMOUNTS. 05 WS-AMOUNT-1 PIC X(12) VALUE "000000.01". 05 WS-AMOUNT-2 PIC X(12) VALUE "000123.45". 05 WS-AMOUNT-3 PIC X(12) VALUE "001234.56". 05 WS-AMOUNT-4 PIC X(12) VALUE "012345.67". 01 WS-AMOUNT-INDEX PIC 9(2). DISPLAY "Report amount formatting with LEADING:" DISPLAY SPACES PERFORM VARYING WS-AMOUNT-INDEX FROM 1 BY 1 UNTIL WS-AMOUNT-INDEX > 4 EVALUATE WS-AMOUNT-INDEX WHEN 1 DISPLAY " Amount 1 original: " WS-AMOUNT-1 INSPECT WS-AMOUNT-1 REPLACING LEADING "0" BY " " DISPLAY " Amount 1 formatted:" WS-AMOUNT-1 WHEN 2 DISPLAY " Amount 2 original: " WS-AMOUNT-2 INSPECT WS-AMOUNT-2 REPLACING LEADING "0" BY " " DISPLAY " Amount 2 formatted:" WS-AMOUNT-2 WHEN 3 DISPLAY " Amount 3 original: " WS-AMOUNT-3 INSPECT WS-AMOUNT-3 REPLACING LEADING "0" BY " " DISPLAY " Amount 3 formatted:" WS-AMOUNT-3 WHEN 4 DISPLAY " Amount 4 original: " WS-AMOUNT-4 INSPECT WS-AMOUNT-4 REPLACING LEADING "0" BY " " DISPLAY " Amount 4 formatted:" WS-AMOUNT-4 END-EVALUATE DISPLAY SPACES END-PERFORM. *> Advanced string trimming function PERFORM-STRING-TRIMMING. 01 WS-TRIM-INPUT PIC X(50). 01 WS-TRIM-OUTPUT PIC X(50). 01 WS-LEADING-SPACES PIC 9(3) VALUE 0. MOVE " This is a test string with spaces " TO WS-TRIM-INPUT MOVE WS-TRIM-INPUT TO WS-TRIM-OUTPUT DISPLAY "String trimming with LEADING:" DISPLAY " Input: '" WS-TRIM-INPUT "'" *> Count leading spaces INSPECT WS-TRIM-OUTPUT TALLYING WS-LEADING-SPACES FOR LEADING SPACES DISPLAY " Leading spaces: " WS-LEADING-SPACES *> Remove leading spaces INSPECT WS-TRIM-OUTPUT REPLACING LEADING SPACES BY LOW-VALUES *> Compact the string (move non-LOW-VALUES to the left) PERFORM COMPACT-STRING DISPLAY " Output: '" WS-TRIM-OUTPUT "'" DISPLAY SPACES. COMPACT-STRING. 01 WS-SOURCE-POS PIC 9(3) VALUE 1. 01 WS-TARGET-POS PIC 9(3) VALUE 1. 01 WS-TEMP-CHAR PIC X. 01 WS-COMPACTED PIC X(50) VALUE SPACES. PERFORM VARYING WS-SOURCE-POS FROM 1 BY 1 UNTIL WS-SOURCE-POS > 50 MOVE WS-TRIM-OUTPUT(WS-SOURCE-POS:1) TO WS-TEMP-CHAR IF WS-TEMP-CHAR NOT = LOW-VALUES MOVE WS-TEMP-CHAR TO WS-COMPACTED(WS-TARGET-POS:1) ADD 1 TO WS-TARGET-POS END-IF END-PERFORM MOVE WS-COMPACTED TO WS-TRIM-OUTPUT. *> Business application examples BUSINESS-APPLICATIONS. DISPLAY "=== BUSINESS APPLICATIONS OF LEADING ===" DISPLAY SPACES PERFORM PRODUCT-CODE-FORMATTING PERFORM INVOICE-NUMBER-CLEANUP PERFORM ADDRESS-FORMATTING. PRODUCT-CODE-FORMATTING. 01 WS-PRODUCT-CODES. 05 WS-PROD-CODE-1 PIC X(15) VALUE "000PROD-12345". 05 WS-PROD-CODE-2 PIC X(15) VALUE "00SERV-67890". 05 WS-PROD-CODE-3 PIC X(15) VALUE "ITEM-11111". DISPLAY "Product code formatting:" DISPLAY " Product 1:" DISPLAY " Original: " WS-PROD-CODE-1 INSPECT WS-PROD-CODE-1 REPLACING LEADING "0" BY " " DISPLAY " Formatted:" WS-PROD-CODE-1 DISPLAY " Product 2:" DISPLAY " Original: " WS-PROD-CODE-2 INSPECT WS-PROD-CODE-2 REPLACING LEADING "0" BY " " DISPLAY " Formatted:" WS-PROD-CODE-2 DISPLAY " Product 3:" DISPLAY " Original: " WS-PROD-CODE-3 DISPLAY " No leading zeros - no change needed" DISPLAY SPACES. INVOICE-NUMBER-CLEANUP. 01 WS-INVOICE-NUMBERS. 05 WS-INV-NUM-1 PIC X(20) VALUE "INV-0000000012345". 05 WS-INV-NUM-2 PIC X(20) VALUE "BILL-000000067890". 01 WS-ZERO-COUNT-1 PIC 9(2) VALUE 0. 01 WS-ZERO-COUNT-2 PIC 9(2) VALUE 0. DISPLAY "Invoice number cleanup:" DISPLAY " Invoice 1:" DISPLAY " Original: " WS-INV-NUM-1 INSPECT WS-INV-NUM-1 TALLYING WS-ZERO-COUNT-1 FOR LEADING "0" AFTER INITIAL "INV-" DISPLAY " Leading zeros after prefix: " WS-ZERO-COUNT-1 INSPECT WS-INV-NUM-1 REPLACING LEADING "0" BY " " AFTER INITIAL "INV-" DISPLAY " Cleaned: " WS-INV-NUM-1 DISPLAY " Invoice 2:" DISPLAY " Original: " WS-INV-NUM-2 INSPECT WS-INV-NUM-2 TALLYING WS-ZERO-COUNT-2 FOR LEADING "0" AFTER INITIAL "BILL-" DISPLAY " Leading zeros after prefix: " WS-ZERO-COUNT-2 INSPECT WS-INV-NUM-2 REPLACING LEADING "0" BY " " AFTER INITIAL "BILL-" DISPLAY " Cleaned: " WS-INV-NUM-2 DISPLAY SPACES. ADDRESS-FORMATTING. 01 WS-ADDRESS-LINES. 05 WS-ADDR-LINE-1 PIC X(40) VALUE " 123 MAIN STREET". 05 WS-ADDR-LINE-2 PIC X(40) VALUE " APARTMENT 4B". 05 WS-ADDR-LINE-3 PIC X(40) VALUE "ANYTOWN, NY 12345". DISPLAY "Address formatting:" DISPLAY " Address line 1:" DISPLAY " Original: '" WS-ADDR-LINE-1 "'" INSPECT WS-ADDR-LINE-1 REPLACING LEADING SPACES BY "*" DISPLAY " Marked: '" WS-ADDR-LINE-1 "'" DISPLAY " Address line 2:" DISPLAY " Original: '" WS-ADDR-LINE-2 "'" INSPECT WS-ADDR-LINE-2 REPLACING LEADING SPACES BY "*" DISPLAY " Marked: '" WS-ADDR-LINE-2 "'" DISPLAY " Address line 3:" DISPLAY " Original: '" WS-ADDR-LINE-3 "'" DISPLAY " No leading spaces - no change needed" DISPLAY SPACES.

LEADING Use Cases

Data Cleanup
  • • Remove leading spaces from names
  • • Strip leading zeros from IDs
  • • Clean leading punctuation
  • • Standardize data format
  • • Prepare data for processing
Report Formatting
  • • Format numeric displays
  • • Align text in reports
  • • Clean reference numbers
  • • Standardize field presentation
  • • Improve readability

Interactive Tutorial

Hands-On Exercise: Data Cleanup with LEADING
Practice using LEADING for string cleanup and formatting operations

Exercise 1: Customer Name Cleanup

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
01 WS-CUSTOMER-NAMES. 05 WS-NAME-1 PIC X(30) VALUE " JOHN SMITH". 05 WS-NAME-2 PIC X(30) VALUE " JANE DOE". 05 WS-NAME-3 PIC X(30) VALUE "ROBERT JOHNSON". 01 WS-SPACE-COUNT PIC 9(2) VALUE 0. PROCEDURE DIVISION. CLEANUP-NAMES. PERFORM VARYING WS-INDEX FROM 1 BY 1 UNTIL WS-INDEX > 3 MOVE 0 TO WS-SPACE-COUNT EVALUATE WS-INDEX WHEN 1 INSPECT WS-NAME-1 TALLYING WS-SPACE-COUNT FOR LEADING SPACES DISPLAY "Name 1 leading spaces: " WS-SPACE-COUNT INSPECT WS-NAME-1 REPLACING LEADING SPACES BY "." WHEN 2 INSPECT WS-NAME-2 TALLYING WS-SPACE-COUNT FOR LEADING SPACES DISPLAY "Name 2 leading spaces: " WS-SPACE-COUNT INSPECT WS-NAME-2 REPLACING LEADING SPACES BY "." WHEN 3 DISPLAY "Name 3 has no leading spaces" END-EVALUATE END-PERFORM.

Exercise 2: Account Number Formatting

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
01 WS-ACCOUNT-NUMBERS. 05 WS-ACCT-1 PIC X(15) VALUE "00000123456". 05 WS-ACCT-2 PIC X(15) VALUE "00987654321". 05 WS-ACCT-3 PIC X(15) VALUE "00000000007". 01 WS-ZERO-COUNT PIC 9(2) VALUE 0. PROCEDURE DIVISION. FORMAT-ACCOUNTS. DISPLAY "Account number formatting:" PERFORM VARYING WS-INDEX FROM 1 BY 1 UNTIL WS-INDEX > 3 MOVE 0 TO WS-ZERO-COUNT EVALUATE WS-INDEX WHEN 1 DISPLAY "Account 1 original: " WS-ACCT-1 INSPECT WS-ACCT-1 TALLYING WS-ZERO-COUNT FOR LEADING "0" DISPLAY "Leading zeros: " WS-ZERO-COUNT INSPECT WS-ACCT-1 REPLACING LEADING "0" BY " " DISPLAY "Account 1 formatted:" WS-ACCT-1 WHEN 2 DISPLAY "Account 2 original: " WS-ACCT-2 INSPECT WS-ACCT-2 REPLACING LEADING "0" BY " " DISPLAY "Account 2 formatted:" WS-ACCT-2 WHEN 3 DISPLAY "Account 3 original: " WS-ACCT-3 INSPECT WS-ACCT-3 REPLACING LEADING "0" BY " " DISPLAY "Account 3 formatted:" WS-ACCT-3 END-EVALUATE DISPLAY SPACES END-PERFORM.

Best Practices

Knowledge Check

Test Your Understanding

Question 1: LEADING Behavior

What happens when INSPECT with LEADING encounters a non-matching character?

Answer: LEADING stops processing when it encounters the first character that doesn't match the specified character or pattern. It only processes consecutive matching characters from the beginning of the string.

Question 2: TALLYING vs REPLACING

What's the difference between TALLYING and REPLACING with LEADING?

Answer: TALLYING counts the number of leading characters and stores the count in a variable, while REPLACING actually changes the leading characters to a replacement character or string.

Question 3: Business Applications

What are common business uses for LEADING operations?

Answer: Common uses include removing leading zeros from account numbers, trimming leading spaces from names and addresses, cleaning up product codes, and formatting numeric displays for reports.

Related Pages