sams_teach_yourself_cobol_in_24_hours_-_hour_5_procedure_division

Sams Teach Yourself COBOL in 24 Hours - Hour 5 Procedure Division

Return to Teach Yourself COBOL in 24 Hours, COBOL bibliography, COBOL, COBOL DevOps, Awesome COBOL, Awesome IBM Mainframe, IBM Mainframe development, IBM Mainframe bibliography, Fortran

“ (TYCb24H 1998)

Hour 6 Manipulating Data

You have now learned some simple data manipulation statements. You can do some basic math and move fields around. You have learned some basic features to allow you to interface with the user. Now it is time to cover some more advanced statements used to work with data fields.

In its role at fulfilling the needs of business, COBOL works with myriad data. It must handle the mathematics of business and be able to process textual data. Textual data consists of items such as names, addresses, and telephone numbers. Textual data can also contain descriptions of other important data, such as medical procedures. COBOL comes with a suite of very powerful tools to handle and manipulate this type of data. In this hour you learn about

• The Accept statement

• The Initialize statement

• The Inspect statement

Reference modification

The Accept Statement Some uses of the Accept statement, in the area of communicating with the user, have already been covered. In addition, you can use the Accept statement for more than just retrieving user input. You may accept data either from the user or from the operating system. You have already seen the method for accepting input from a screen definition.

Accepting from the User When interfacing with the user, the Accept statement moves data from a specific device into a data field. In the absence of a specifically coded device, the default device for the Accept is used. For example:

000033 Accept Some-Field. This Accept statement moves data from the default device, normally the console or current user terminal, into the data item Some-Field.

The different items that can be accepted using the Accept statement vary from compiler to compiler. Different computers have different devices and different requirements.

One of the interesting uses of the Accept with the Fujitsu compiler is to allow the programmer to retrieve command-line arguments. These are the items passed to the program on the command line. For example, if your program is CHAPT06A.EXE and you type CHAPT06A MyName, the command-line argument is MyName. Here is an example of how Fujitsu allows you to use the Accept statement to retrieve the command-line argument.

000001 @OPTIONS MAIN 000002 Identification Division. 000003 Program-Id. Chapt06a. 000004* Command Line Argument 000005 Environment Division. 000006 Configuration Section. 000007 Source-Computer. IBM-PC. 000008 Object-Computer. IBM-PC. 000009 Special-Names. 000010 Argument-Value Is Command-line. 000011 Data Division. 000012 Working-Storage Section. 000013 01 Command-Line-Argument Pic X(80). 000014 Procedure Division. 000015 Chapt06a-Start. 000016 Accept Command-Line-Argument From Command-Line 000017 DisplayCommand Line: ” Command-Line-Argument 000018 Stop Run 000019 . Take special notice of the Special-Names paragraph. The name Argument-Value is a Fujitsu provided special name. Using this method of assigning a value in the Special-Names paragraph sets up most of the special items that may be accepted.

Enter and compile this program. When you run it, add an argument after the Chapt06a.exe on the command line. Notice that if you add more than one word, only the first is displayed. You may code multiple Accept statements to retrieve all the command-line arguments. In addition, Fujitsu provides a special name, Argument-Number, that can be used to determine the number of command-line arguments.

Accepting Data from the System A number of very useful, predefined Accept variables are part of the COBOL standard. These relate to retrieving the system date, time, and day of the week. Two date formats are supported. One is the Gregorian date, and the second is the Julian date. The Gregorian date is the type of date you are used to seeing; its numbers correspond to the month, day, and year. The Julian date is made up of the year and the number of the days in the year to the present date. For example, January 1 is day 1. December 31, during a year that is not a leap year, is day 365. If the year is a leap year, December 31 is day 366.

The following examples show the syntax for these Accept statements.

000045 Accept Date-Field From Date. 000046 Accept Day-Field From Day. 000047 Accept Week-Day From Day-Of-Week. 000048 Accept Time-Of-Day From Time. The field that Date is accepted into must be a six-digit numeric data field. The format of the input is YYMMDD, where YY is the current two-digit year, MM is the current month where 01 is January and 12 is December, and DD is the day of the month.

The field that Day is accepted into must be a five-digit numeric data field. The format of the input is YYDDD, where is the current two-digit year and DDD is the current Julian day.

Image

When working with the current date, try to avoid using the Accept statement with Date and Day. The reason is that only a two-digit year is returned. To get the current full four-digit year, use the intrinsic function Current Date, which is discussed in detail in Hour 21, “Date Manipulation.”

The field that Day-Of-Week is accepted into must be a single-digit numeric field. If the field contains 1, the current weekday is Monday, 2 is Tuesday, and so on.

The field that Time is accepted into must be an eight-digit numeric field. The format of the time is HHMMSShh, where HH corresponds to the hour in military time format, for example: 01 is 1 a.m., 13 is 1 p.m. MM corresponds to the minutes, SS corresponds to the seconds, and hh to the hundredths of seconds.

The Initialize Statement As you write programs, you may want to reset the values of your fields. If you are accumulating totals for a report, after you print a total you may want to clear your detail fields. After retrieving a screen from the user, you may want to clear the screen of all user-entered values. Writing individual Move statements to erase the values in the fields can be a cumbersome exercise.

The Initialize statement is a very powerful statement for setting the initial values of your data fields. It can be a very fast and easy way to set the value for a data item or series of data items.

Image

Exercise caution when using the Initialize statement against items in Working Storage to which you have assigned a value with the value clause. Initialize sets their value as appropriate for the type of field and does not set their content to that specified in the value clause.

For example, the following group is defined in the Working-Storage Section of your program.

000040 01 Working-Variables. 000041 03 Numeric-Variables. 000042 05 First-Numeric-Variable Pic 9(5). 000043 05 Second-Numeric-Variable Pic 9(5). 000044 03 Alphanumeric-Variables. 000045 05 First-Alphanumeric-Variable Pic X(20) value all “*”. 000046 05 Second-Alphanumeric-Variable Pic X(20). First, notice line 45. The field contains 20 * characters. The other fields can have any value that your program has moved into the fields. If you wish to reset all these fields, there are a couple of choices.

You may move spaces to the Working-Variables field. However, this Move places invalid data into the numeric fields. Another solution is to code multiple Move statements to move zeros to the numeric fields and spaces to the alphanumeric fields. In that case, you must explicitly move something to each field name. A better option is to code an Initialize statement.

000101 Initialize Working-Variables. When the Initialize is performed, each field in the group, at its elementary level, is either set to zeros or spaces, depending on the type of field. Numeric and numeric edited fields are set to zeros, and alphanumeric fields are set to spaces, just as if a Move statement had been performed with each field as the receiving field.

The Initialize verb can also target specific field types within a group. If you have a group defined, do not want to group like field types together, and only want to initialize the numeric fields in the group, you can still use Initialize if you just add the Replacing clause.

000102 Initialize Working-Variables Replacing Numeric Data By Zeros. The Replacing clause allows you to specify the type of field, within a group, on which the Initialize is to operate. In this example, only the numeric elementary fields defined within the Working-Variables group are set to zero. You may specify Alphanumeric, Alphanumeric-Edited, Numeric, or Numeric-Edited after the word Replacing.

Another powerful feature is the ability to use Initialize to set fields of various types to unique values other than spaces and zeros. If you want to change First-Alphanumeric-Variable to contain all asterisks again, you can code as follows:

000103 Initialize First-Alphanumeric-Variable 000104 Replacing Alphanumeric Data By “********************” An alternative to coding all the asterisks, and potentially miscounting, is to use All “*” or the Move statement with the All clause. For example:

000105 Initialize First-Alphanumeric-Variable 000106 Replacing Alphanumeric Data By All “*” or

000107 Move All “*” To First-Alphanumeric-Variable Notice that the Initialize is not restricted for use against Group Level items, although in this instance, a simple Move will accomplish the same thing. If, however, you wanted all alphanumeric fields within a group to contain the asterisks, then Initialize makes more sense.

Image

When using Initialize with Replacing, remember that the field or literal you specify after the word By is not repeated within the object of the Initialize. For example, if you code Initialize First-Alphanumeric-Variable to “*”, the result will be “*” and not “********************” as you might expect.

You are not restricted to literals in the Replacing phrase. You may also Initialize a field with the contents of another field.

000102 Initialize Working-Variables Replacing Numeric Data By Field-1. In this example, every numeric field defined under the group Working-Variables is initialized to the current value of Field-1.

The Inspect Statement One of the more versatile and powerful COBOL data manipulation verbs is the Inspect statement. Inspect can be used for anything from testing a field for specific contents to converting those contents to other values. The Inspect statement may be coded in several formats.

The first usage allows you to count the occurrences of a particular character or characters within a field. For example, to determine whether a data item contains a comma, you can use the Inspect statement to count the commas in the field.

000103 Inspect Data-item tallying Work-Counter For All “,”. After this statement is executed, Work-Counter contains the number of commas in Data-Item. For example, if Data-Item contains “Hubbell, Darlene”, Work-Counter's value is 1.

What if you want to count all of the times that the letter b occurs in the last name? You need to stop counting when the, is encountered. The Inspect statement makes this very easy by allowing you to add the phrase, Before Initial.

000104 Inspect Data-item Tallying Work-Counter For All “b” Before 000105 Initial “,”. In this example, the result stored in Work-Counter is 2. In addition to allowing you to code the Before Initial clause, Inspect also supports the After Initial clause. You can use After Initial to count the occurrence of a character or characters after the comma.

Instead of counting all occurrences of a single character, you may want to determine the number of leading characters.

Leading characters precede any other character in a field. For example, if a field contains “****ABC”, it contains four leading asterisks. If you want to determine the number of leading characters in a field, you might code as follows:

000105 Inspect Data-Item Tallying Work-Counter For Leading “*”. This format of Inspect also determines the number of total characters in a field that meet specific conditions. You can determine the number of characters that occur before or after a comma, for example. Using the earlier example, you can use Inspect to determine the length of the last name.

000106 Inspect Data-Item Tallying Work-Counter For Characters Before 000107 Initial “,”. If you want to count the number of characters after the comma, you may change the Before Initial to After Initial.

Image

The word Initial is optional. You may omit it when coding the Before or After phrases.

A second format of the Inspect statement allows you to replace characters in a field with other characters. This tool is very powerful for editing data fields into specific formats. For example, if you have a date field that was entered with “/” characters separating the values and you needed to replace the “/” with a “-”, you can use the Inspect statement. Assume your date field contains “01/04/1999”.

000107 Inspect Data-item Replacing All “/” By “-”. You may replace literals or data items with either literals or data items. The statement is very flexible. The following example uses the Inspect statement to format a telephone number for display.

Listing 6.1 Telephone Number Format

000001 @OPTIONS MAIN 000002 Identification Division. 000003 Program-Id. Chapt06b. 000004* Telephone Number Format 000005 Environment Division. 000006 Configuration Section. 000007 Source-Computer. IBM-PC. 000008 Object-Computer. IBM-PC. 000009 Data Division. 000010 Working-Storage Section. 000011 01 Phone-Number. 000012 03 Area-code Pic XXX Value “409”. 000013 03 Prefix-Num Pic XXX Value “555”. 000014 03 Last-Four Pic X(4) Value “1212”. 000015 01 Formatted-Number Pic X(14) Value “(XXX) YYY-ZZZZ”. 000016 01 Formatted-Alternate Pic X(14) Value “(XXX) XXX-XXXX”. 000017 000018 Procedure Division. 000019 Start-Of-Program. 000020 Inspect Formatted-Number 000021 Replacing All “XXX” By Area-Code 000022 All “YYY” By Prefix-Num 000023 All “ZZZZ” By Last-Four 000024 Display Formatted-Number 000025 Inspect Formatted-Alternate 000026 Replacing First “XXX” By Area-Code 000027 First “XXX” By Prefix-Num 000028 First “XXXX” By Last-Four 000029 Display Formatted-Alternate 000030 Stop Run 000031 . Notice that multiple replacing statements may appear within an Inspect statement, and they are processed in order. The first Inspect in line 21 replaces all occurrences of the text. The second Inspect statement replaces only the first occurrence of the text: in the example, “XXX” was used repeatedly, and a single Replacing would have changed all three sets of “XXX” to the area code.

The Leading phrase may be used instead of All if you need to change only the leading characters to something else.

The Characters phrase is also valid in this format of the Inspect statement. It can be used to change every character in a field to another character. You can use Inspect to change all characters in a field to “*” characters.

000035 Inspect Data-Field Replacing Characters By “*”. This statement replaces every character in a field, regardless of the length of the field, with asterisks.

The third format of the Inspect statement allows you to count characters using Tallying and to replace characters using Replace. This format can be useful to count the number of characters or occurrences you have changed.

000036 Inspect Data-Field Tallying Character-Count For All Spaces 000037 After “-” Replacing All Spaces After “-” by “X”. The preceding example converts all spaces that appear after a “-” in a field with the letter X. The Character-Count field contains the number of spaces that were changed to the letter X.

The final format of the Inspect statement allows you to convert characters from one value to another. Although similar to Replacing, the Converting allows you to specify a string of single characters (data item or literal) that will be converted to the values specified in a second string. This can be used to convert a name, or portion of a name, from lowercase to uppercase letters. For example, to change “Hubbell, Darlene” to “HUBBELL, DARLENE”, code the following:

000038 Inspect Data-Field Converting “abcdefghijklmnopqrstuvwxyz” To 000039 “ABCDEFGHIJKLMNOPWRSTUVWXYZ”. Every time a character in the string of values on the left of the To is encountered, it is changed to the matching character on the right of the To. If you want to convert only the last name from the example and leave the first name alone, code the following:

000038 Inspect Data-Field Converting “abcdefghijklmnopqrstuvwxyz” To 000039 “ABCDEFGHIJKLMNOPWRSTUVWXYZ” 000040 Before initial “,”. Do you remember playing code games as a kid? Remember the simple substitution codes? The letters of the alphabet were rearranged to make a code. If you knew which letters of the alphabet corresponded to the letters in the code, you could solve the puzzle. The Inspect statement with Converting works in a similar fashion, performing a single substitution for each character.

In addition to being able to restrict the conversion by specifying the Before Initial phrase, you may also specify After Initial.

Reference Modification Reference modification is a method provided to reference a portion of a data item. Reference modification allows you to use a portion of a field as if it were its own elementary item. You may use reference modification on alphanumeric fields or on numeric fields that are Usage Display. The way you specify reference modification in your program is to place a starting position and length in parenthesis separated by a colon, after your data item.

000041 Display Data-Item (1:4). If the Data-Item field contains “Inventory”, this Display statement displays “Inve”. The first number denotes the starting position, and the one after the colon specifies the length. You may use reference modification with virtually any COBOL statement that references a data item.

The numbers used to define the starting position and length may be in the form of numeric literals as in the example, data items, or arithmetic expressions. If an arithmetic expression is used, the values must be positive. The length item after the colon may be omitted. If it is omitted, then the remaining characters to the end of the data item are used.

000042 Display Data-Item (5:). Using the same Data-Item value as the previous example, this example displays “ntory”.

Image

Reference modification is a very powerful feature. It can be used for many things. However, it can also be abused. Don't use reference modification to further divide a data item when it can be more clearly defined as a group item made up of elementary items. For example, if a data item consists of last and first name, define a group:

01 Full-Name.

03 Last-Name Pic X(30).

03 First-Name Pic X(20).

If you want to display the last name, code the following:

Display Last-Name.

Don't use reference modification. Display Full-Name (1:30) is not nearly as clear.

Using What You Have Learned in a Program It is time to put these pieces together and accomplish a programming task. For this example, you develop a program that accepts a full name, with the first name separated from the last by a comma, and an email address. The first and last names are split into separate fields, and the email address is converted to lowercase. The results are then displayed.

Open the Fujitsu Editor and create a new file in your TYCOBOL folder. Code the normal COBOL statements required to identify the program.

000001 @OPTIONS MAIN 000002 Identification Division. 000003 Program-Id. Chapt06c. 000004* Name and E-mail Edit 000005 Environment Division. 000006 Configuration Section. 000007 Source-Computer. IBM-PC. 000008 Object-Computer. IBM-PC. Notice the use of the comment in line 4 to identify the purpose of the program. Now code the Data Division and the Working-Storage Section. You need fields to hold the input and to display the output. You also need two numeric fields to contain some numbers that are used in your program.

000009 Data Division. 000010 Working-Storage Section. 000011 01 Screen-Items. 000012 03 Name-Entry Pic X(40) Value Spaces. 000013 03 E-mail Pic X(30) Value Spaces. 000014 03 Last-Name Pic X(30) Value Spaces. 000015 03 First-Name Pic X(30) Value Spaces. 000016 01 Work-Number Pic 99 Value Zeros. 000017 01 Work-Number-1 Pic 99 Value Zeros. Take special note of the fact that initial values were assigned to these fields. Otherwise, the initial display of the screen items might contain junk characters. Also, note that only one field is defined for E-mail. Because you are not splitting the E-mail field into two fields, like the name, you need only the one field.

Next, code the Screen Section for displaying and accepting the entered values.

000018 Screen Section. 000019 01 Name-Entry-Screen 000020 Blank Screen, Auto 000021 Foreground-Color Is 7, 000022 Background-Color Is 1. 000023* 000024 03 Screen-Literal-Group. 000025 05 Line 01 Column 30 ValueName and E-mail Entry” 000026 Highlight Foreground-Color 4 Background-Color 1. 000027 05 Line 05 Column 05 ValueName: ”. 000028 05 Line 06 Column 05 Value “E-mail: ”. 000029 05 Line 08 Column 05 ValueLast: ”. 000030 05 Line 09 Column 05 ValueFirst: ”. 000031 03 Reverse-Video-Group Reverse-Video. 000032 05 Line 05 Column 13 Pic X(40) Using Name-Entry. 000033 05 Line 06 Column 13 Pic X(30) Using E-mail. 000034 05 Line 08 Column 13 Pic X(30) From Last-Name. 000035 05 Line 09 Column 13 Pic X(30) From First-Name. In the screen definition, note the use of the Using phrase for the Name-Entry and E-mail but the From phrase for Last-Name and First-Name. Because you will be splitting the name entered into these two fields, you don't want the user to enter any data into those fields.

The Procedure Division is coded next, up to the point of displaying and accepting the screen.

000036 Procedure Division. 000037 Chapt06c-Start. 000038 Display Name-Entry-Screen 000039 Accept Name-Entry-Screen The next step is to determine how many characters in the Name-Entry field appear before the comma. Then you can move those characters to the new Last-Name field. Notice the use of the comment in the following code, used to explain what you are trying to do.

000040* Split the first and last name out into separate fields 000041 Inspect Name-Entry Tallying Work-Number 000042 For Characters Before “,” 000043 Move Name-Entry (1:Work-Number) To Last-Name The Inspect statement in lines 41 and 42 counts the number of characters that appear before the comma. This number is stored in the Work-Number field. Line 43 uses reference modification to move this portion of the Name-Entry field into the Last-Name. Reference modification causes the characters starting in position 1 and extending for a length of the value of Work-Number to be moved into the Last-Name field.

The first part is done; now you need to move the last name into the Last-Name field. To do that, you need to make sure that the position you start working on in the Name-Entry field is the first position after the comma. To do that, add 2 to Work-Number because the value of Work-Number is the number of characters in the field that appear before the comma.

Add 2 to Work-Number The user may have entered the name with a space after the comma, multiple spaces after the comma, or no spaces. You want the First-Name field to start in the left-most position, also called left-justified, so you need to exclude any leading spaces in the first name portion of the input field.

000044* You need to exclude the leading spaces, after the comma 000045 Inspect Name-Entry (Work-Number:) 000046 Tallying Work-Number-1 For Leading Spaces 000047 Move Name-Entry (Work-Number + Work-Number-1:) To First-Name The Inspect statement in line 45 uses reference modification on the Name-Entry input field to count the number of spaces that appear after the comma but before any other character. Note in the reference modification that only the : is coded, not a length. This format causes the Inspect to start at the position defined in Work-Number and end at the end of the field.

When you know the number of spaces, you can then move the portion of Name-Entry that is the first name into the First-Name field. You do so by using reference modification. Within the reference modification, the starting position is determined by a numeric expression. This expression is the sum of Work-Number, which is now equal to the first position after the comma, and Work-Number-1, which contains the number of spaces that appear after the comma. This step positions the starting point for the move on the first nonblank character that appears after the comma in the input field.

Now that the first and last names are moved, it's time to convert the email address to lowercase. This step is accomplished with a simple Inspect statement.

000048*Change the e-mail address to all lower case letters. 000049 Inspect E-mail Converting “ABCDEFGHIJKLMNOPQRSTUVWXYZ” 000050 To “abcdefghijklmnopqrstuvwxyz” Finally, you need to display the results of the program for the user and then end the program. The screen display output from this program is shown in Figure 6.1.

000346* Show the results 000347 Display Name-Entry-Screen 000348 Stop Run 000349 .

Figure 6.1 Output from Chapt06C.

Image

Summary In this hour, you learned the following:

• How to use the Accept statement to get the date and time from the computer

• How to use the Accept statement to find the parameters the user entered on the command line

• How to use the Initialize statement to reset the values in various fields

• How to use the Inspect statement to count characters in a field

• How to use the Inspect statement to convert data in a field from one value to another

• How to use reference modification to address a portion of a data field

• How to combine these elements in a program to perform a useful function

Q&A Q Why do you need to be careful when accepting the date from the system in an Accept statement?

A The Accept statement returns only the last two digits of the current year. Another COBOL function that returns the entire year is discussed in Hour 21.

Q Are the special names that accept data from the command line the same for all COBOL compilers?

A No. Although most are similar, the environments, or computers and operating systems, on which the compilers run have different requirements. The COBOL standard allows implementers some leeway in defining the interface to these special areas of their environment. You should review your compiler documentation, specifically the language reference, to determine which special names are available for your use.

Q Will the Initialize statement reset a data item to contain the value that was specified as a value in the picture clause definition of the data item?

A No. The Initialize statement, by default, sets alphanumeric data fields to spaces and numeric data fields to zeros. However, you can specify which values to use when you code the Initialize statement. Doing so allows you to place specific values in specific field types.

Q Can inspect count the number of times that more than a single character appears in a data item? For example the combination “JR”?

A Yes. The Inspect statement is not limited to looking at only a single character. You may code an Inspect statement as follows: Inspect Data-Item Tallying Numeric-Work For All “JR”.

Q Can reference modification be used on numeric data fields as well as on alphanumeric data fields?

A Yes. However, the numeric data field must be defined as Usage Display, which is the default usage. If you have specified any other usage, such as COMP-3 or Binary, then reference modification may not be used.

Q Can reference modification be used on Group Level items as well as elementary items?

A Yes. Reference modification treats Group Level items as alphanumeric items.

Workshop To help reinforce your understanding of the material presented in this hour, refer to the sectionQuiz and Exercise Questions and Answers” that can be found on the CD. This section contains quiz questions and exercises for you to complete, as well as the corresponding answers.

[[Fair Use]] [[Source]]s

COBOL: COBOL Fundamentals, COBOL Inventor - COBOL Language Designer: 1959 by Howard Bromberg, Norman Discount, Vernon Reeves, Jean E. Sammet, William Selden, Gertrude Tierney, with indirect influence from Grace Hopper, CODASYL, ANSI COBOL, ISO/IEC COBOL; Modern COBOL - Legacy COBOL, IBM COBOL, COBOL keywords, COBOL data structures - COBOL algorithms, COBOL syntax, Visual COBOL, COBOL on Windows, COBOL on Linux, COBOL on UNIX, COBOL on macOS, Mainframe COBOL, IBM i COBOL, IBM Mainframe DevOps, COBOL Standards, COBOL Paradigms (Imperative COBOL, Procedural COBOL, Object-Oriented COBOL - COBOL OOP, Functional COBOL), COBOL syntax, COBOL installation, COBOL containerization, COBOL configuration, COBOL compilers, COBOL IDEs, COBOL development tools, COBOL DevOps - COBOL SRE, COBOL data science - COBOL DataOps, COBOL machine learning, COBOL deep learning, COBOL concurrency, COBOL history, COBOL bibliography, COBOL glossary, COBOL topics, COBOL courses, COBOL Standard Library, COBOL libraries, COBOL frameworks, COBOL research, Grace Hopper, COBOL GitHub, Written in COBOL, COBOL popularity, COBOL Awesome list, COBOL Versions. (navbar_cobol)


© 1994 - 2024 Cloud Monk Losang Jinpa or Fair Use. Disclaimers

SYI LU SENG E MU CHYWE YE. NAN. WEI LA YE. WEI LA YE. SA WA HE.


sams_teach_yourself_cobol_in_24_hours_-_hour_5_procedure_division.txt · Last modified: 2024/04/28 03:37 (external edit)