|
|||||
|
This suite of programs will describe how to parse a data string using COBOL. The parsing routine uses reference modification to identify the position of the first significant character after a delimiter character. This approach compensates for multiple leading or embedded delimiter characters. For example, if the delimiter character is a space then leading spaces will be ignored (or removed) and multiple, embedded spaces will be treated as a single space. A new field (or string) is created with the leading spaces removed and all multiple embedded spaces reduced to a single space. The length of the actual text within the output field is also provided.
String manipulation or parsing is quite often considered something that cannot be done using COBOL. Prior to the introduction of Reference Modification in the ANSI/85 standard for COBOL the scanning or parsing of a field at the character-by-character level was very difficult and troublesome. Throughout the years I have been asked to provide clients with mainframe assembler routines to do various functions. When I ask why they want an assembler routine the answer is usually, "I have been told this cannot be done in COBOL."
I have heard this statement many times and in a number of instances it is simply not true. An assembler routine would probably be smaller and run faster. However, if the task could be accomplished in a reasonable manner using COBOL then the cost, effort, frustration and ongoing support of an assembler routine in a COBOL programming shop may not be worth these advantages. This is especially true if assembler expertise is not readily available.
The COBOL language does have a STRING and UNSTRING function. The STRING statement provides for the concatenation of the partial or complete contents of two or more data items into a single data item. The UNSTRING statement causes contiguous data in a sending field to be separated and placed into multiple receiving fields. The STRING and UNSTRING capability works quite well for combining and separating keywords delimited by spaces (or some other delimiting character). For more complex scanning and parsing of a field at the character level it may be best to take an alternative approach similar to what is presented in this example.
This example provides a demonstration program and a callable program to parse a field or data string. Both COBOL programs were written and tested using the COBOL/2 dialect. Also, both COBOL programs will work with COBOL for MVS and COBOL/370. A JCL member is provided to run the job as an MVS (or OS/390) batch job on an IBM mainframe or as a project with Micro Focus Mainframe Express (MFE) running on a PC with Windows (refer to http://www.microfocus.com).
This technique could also be used to do character (or byte) access and manipulation. The DISPLAY function of COBOL is used by the demonstration program to display the results of the parsing routine.
In the world of programming there are many ways to solve a problem. This suite of programs is provided as a COBOL example of one of the possible solutions to the problem of parsing a data field.
Note: The source code for this example is available from the SimoTime Library under Download Directory at www.simotime.com
The callable interface is as follows. The following data structure needs to be defined in Working Storage. A copy file (PASSPARS.CPY) is provided with this suite of sample programs.
01 PRS-PARAMETERS.
05 PRS-REQUEST PIC X VALUE '0'.
05 PRS-STATUS PIC 9(4).
*> Provided by the calling program...
05 PRS-DELIMITER PIC X VALUE SPACE.
05 PRS-KEEP-NULL-FIELDS PIC X VALUE 'N'.
05 PRS-SUSPEND PIC X VALUE 'N'.
05 PRS-SUSPEND-BYTE PIC X VALUE SPACE.
05 PRS-TERMINATOR PIC X VALUE 'N'.
05 PRS-TERMINATOR-BYTE PIC X VALUE SPACE.
05 PRS-BUFFER-SIZE PIC 9(4) VALUE 1024.
05 PRS-BUFFER PIC X(1024).
05 PRS-TABLE-MAX PIC 9(4) VALUE 64.
*> Updated by the parsing routine...
05 PRS-NUMBER-OF-ITEMS PIC 9(4) VALUE 0.
05 PRS-LAST-SIG-BYTE PIC 9(4) VALUE 0.
05 PRS-OFFSET OCCURS 64 TIMES
PIC 9(4) VALUE 0.
05 PRS-LENGTH OCCURS 64 TIMES
PIC 9(4) VALUE 0.
To call the parsing routine (SIMOPARS) the fields for the pass area need to be initialized. The following shows an example of initializing the pass area fields and making the call to the parsing routine.
* Prepare control items for parsing.
MOVE '0' TO PRS-REQUEST
MOVE ' ' TO PRS-DELIMITER
MOVE 'N' TO PRS-TERMINATOR
MOVE ' ' TO PRS-TERMINATOR-BYTE
ADD 32 TO ZERO GIVING PRS-TABLE-MAX
ADD 128 TO ZERO GIVING PRS-BUFFER-SIZE
* Move the data string to the parsing buffer and call
* the parsing routine.
MOVE TASK-01-DATA TO PRS-BUFFER
CALL 'SIMOPARS' USING PRS-PARAMETERS
The following is a brief description of each of the fields used within the pass area (PRS-PARAMETERS).
| Field Name | Description |
| PRS-REQUEST | Type of request, must be zero (0) for parsing. This item is provided by the calling program and is not modified by the parsing routine. |
| PRS-STATUS | This is an indicator as to the success or failure of the request. A value of zero (0000) indicates the routine was successful. A non-zero value indicates a failure. |
| PRS-DELIMITER | This is a one byte character that is used as a delimiter. This item is provided by the calling program and is not modified by the parsing routine. |
| PRS-KEEP-NULL-FIELDS | This must be a Yes (Y) or No (N) value (Y) will indicate a Yes, keep null field (N) will indicate a No and null fields will be dropped. |
| PRS-SUSPEND | This must be a Yes (Y) or No (N) value to indicate that the suspend/resume function of checking for a delimiter is active. |
| PRS-SUSPEND-BYTE | If the previous parameter is (Y) this byte will be used to suspend or resume the testing for a delimiter character. |
| PRS-TERMINATOR | This must be a Yes (Y) or No (N) value to indicate a character that will be used for termination of the parsing. If (Y) then the next parameter (PRS-TERMINATOR-BYTE) is used as a termination character. If (N) the buffer will be parsed according to the size (PRS-BUFFER-SIZE) of the buffer or maximum number of table entries (PRS-TABLE-MAX). This item is provided by the calling program and is not modified by the parsing routine. |
| PRS-TERMINATOR-BYTE | If the previous parameter (PRS-TERMINATOR) is (Y) this byte will be used to terminate the parsing when it is encountered in the data string. This item is provided by the calling program and is not modified by the parsing routine. |
| PRS-BUFFER-SIZE | The size of the data string to be parsed and must be a value of 1-1024. This item is provided by the calling program and is not modified by the parsing routine. |
| PRS-BUFFER | This is the data string or field that will be parsed. This item is provided by the calling program and is not modified by the parsing routine. |
| PRS-TABLE-MAX | Maximum number of table entries available for the POSITION/SIZE values of the words in the input buffer. This item is provided by the calling program and is not modified by the parsing routine. |
| PRS-NUMBER-OF-ITEMS | This is the number of keywords found in the input data string. This item is provided by the parsing routine. |
| PRS-LAST-SIG-BYTE | This is the position of the last significant (or non space if spaces are the delimiter) byte in the field. This is also the length of the text string within the field. |
| PRS-POSITION | This is a table of 64 elements of 4-bytes. The value is either zero (0) or the size of a keyword within the input data string. This item is provided by the parsing routine. |
| PRS-SIZE | This is a table of 64 elements of 4-bytes. The value is either zero (0) or the position of a keyword within the input data string. The first position within the buffer is one (1). This item is provided by the parsing routine. |
The following is the Windows Command file (CBLRMPE1.CMD) that is required to run as a job on a PC using Micro Focus Net Express.
@echo OFF
rem * *******************************************************************
rem * This program is provided by: *
rem * SimoTime Enterprises, LLC *
rem * (C) Copyright 1987-2008 All Rights Reserved *
rem * Web Site URL: http://www.simotime.com *
rem * e-mail: helpdesk@simotime.com *
rem * *******************************************************************
rem *
rem * Text - COBOL doing text parsing with reference modification
rem * Author - SimoTime Enterprises
rem * Date - November 11, 2003
rem * Version - 06.07.16
rem *
rem * This set of programs illustrate the use a COBOL program to use
rem * reference modification to parse a string of text by keyword.
rem *
rem * When running with Net Express the IBMCOMP an NOTRUNC directives
rem * will be required to maintain compatability with the mainframe
rem * format and field sizes for binary fields.
rem *
rem * This technique provides for the use of a single COBOL source
rem * program that will run on OS/390, Windows or Unix.
rem *
rem * This set of programs will run on a Personal Computer with Windows
rem * and Micro Focus Net Express.
rem *
rem * ************
rem * * CBLRMPE1 *
rem * ********cmd*
rem * *
rem * ************ ************ ************
rem * * call ******* SIMONOTE ******* CONSOLE *
rem * ********exe* ********cmd* * ************
rem * * *
rem * * * ************
rem * * **** SYSLOG *
rem * * *******data*
rem * *
rem * *
rem * ************ ************ ************
rem * * run ******* CBLRMPC1 ******* CONSOLE *
rem * ************ ********cbl* ************
rem * * *
rem * * *
rem * * ************
rem * * * SIMOPARS *
rem * * ********cbl*
rem * ************
rem * * EOJ *
rem * ************
rem *
rem * ********************************************************************
rem * Step 1 of 2 Set the global environment variables...
rem *
setlocal
call Env1PROD
if "%SYSLOG%" == "" set syslog=c:\SimoLIBR\LOGS\SimoTime.LOG
set CmdName=CblRmpE1
rem *
call SimoNOTE "*******************************************************%CmdName%"
call SimoNOTE "Starting JobName %CmdName%"
rem * ********************************************************************
rem * Step 2 of 2 Execute the sample program...
rem *
run CblRmpC1
if ERRORLEVEL = 0 goto :EojAOK
set JobStatus=0010
goto :EojNOK
:EojAOK
call SimoNOTE "Finished JobName %CmdName%, Job Status is %JobStatus%"
goto :End
:EojNOK
call SimoNOTE "ABENDING JobName %CmdName%, Job Status is %JobStatus%"
echo CBLBITC1 is ABENDING>>%BaseLib1%\LOGS\ABENDLOG.TXT
goto :End
:End
call SimoNOTE "Conclude SysLog is %SYSLOG%"
if not "%1" == "nopause" pause
endlocal
The following is the mainframe JCL (CBLRMPJ1.JCL) required to run the mainline program. The JOB and DD statements will need to be modified for unique mainframe requirements.
//CBLRMPJ1 JOB SIMOTIME,ACCOUNT,CLASS=1,MSGCLASS=0,NOTIFY=CSIP1, // COND=(0,LT) //* ******************************************************************* //* This program is provided by: * //* SimoTime Enterprises, LLC * //* (C) Copyright 1987-2006 All Rights Reserved * //* * //* Web Site URL: http://www.simotime.com * //* e-mail: helpdesk@simotime.com * //* ******************************************************************* //* //* Text - COBOL calls COBOL for parsing of a data buffer. //* Author - SimoTime Enterprises //* Date - January 01, 1989 //* //* This set of programs illustrates the use of COBOL for parsing //* a data string or field. //* //* This routine uses reference modification to identify the //* position of the first significant character after the //* delimiter character. This approach compensates for multiple //* leading or embedded delimiter characters. //* //* For example, if the delimiter character is a space then //* leading spaces will be ignored and multiple, embedded spaces //* will be treated as a single space. //* //* This set of programs will run on a mainframe under MVS or on a //* Personal Computer with Windows and Micro Focus Mainframe Express. //* //* ************ //* * CBLRMPJ1 * //* ********jcl* //* * //* * //* ************ ************ //* * CBLRMPC1 *-----* DISPLAY * //* ********cbl* ************ //* * * //* * * ************ //* * *-CALL--* SIMOPARS * //* * ********cbl* //* * //* ************ //* * EOJ * //* ************ //* //* ******************************************************************* //* Step 1 of 1 This is a single step job. //* //CBLRMPX1 EXEC PGM=CBLRMPC1 //STEPLIB DD DSN=SIMOTIME.DEMO.LOADLIB1,DISP=SHR //*
This program (CBLRMPC1.CBL) was written to test and demonstrate the calling of a COBOL program (SIMOPARS.CBL) that does the parsing of a field or data string.
IDENTIFICATION DIVISION.
PROGRAM-ID. CBLRMPC1.
AUTHOR. SIMOTIME ENTERPRISES.
*****************************************************************
* Copyright (C) 1987-2006 SimoTime Enterprises, LLC. *
* *
* All rights reserved. Unpublished, all rights reserved under *
* copyright law and international treaty. Use of a copyright *
* notice is precautionary only and does not imply publication *
* or disclosure. *
* *
* Permission to use, copy, modify and distribute this software *
* for any non-commercial purpose and without fee is hereby *
* granted, provided the SimoTime copyright notice appear on all *
* copies of the software. The SimoTime name or Logo may not be *
* used in any advertising or publicity pertaining to the use *
* of the software without the written permission of SimoTime *
* Enterprises.This software contains confidential information *
* *
* Permission to use, copy, modify and distribute this software *
* for any commercial purpose requires a fee to be paid to *
* Simotime Enterprises. Once the fee is received by SimoTime *
* the latest version of the software will be delivered and a *
* license will be granted for use within an enterprise, *
* provided the SimoTime copyright notice appear on all copies *
* of the software. The SimoTime name or Logo may not be used *
* in any advertising or publicity pertaining to the use of the *
* software without the written permission of SimoTime *
* Enterprises. *
* *
* SimoTime Enterprises makes no warranty or representations *
* about the suitability of the software for any purpose. It is *
* provided "AS IS" without any express or implied warranty, *
* including the implied warranties of merchantability, fitness *
* for a particular purpose and non-infringement. SimoTime *
* Enterprises shall not be liable for any direct, indirect, *
* special or consequential damages resulting from the loss of *
* use, data or projects, whether in an action of contract or *
* tort, arising out of or in connection with the use or *
* performance of this software *
* *
* SimoTime Enterprises *
* 15 Carnoustie Drive *
* Novato, CA 94949-5849 *
* 415.883.6565 *
* *
* RESTRICTED RIGHTS LEGEND *
* Use, duplication, or disclosure by the Government is subject *
* to restrictions as set forth in subparagraph (c)(1)(ii) of *
* the Rights in Technical Data and Computer Software clause at *
* DFARS 52.227-7013 or subparagraphs (c)(1) and (2) of *
* Commercial Computer Software - Restricted Rights at 48 *
* CFR 52.227-19, as applicable. Contact SimoTime Enterprises, *
* 15 Carnoustie Drive, Novato, CA 94949-5849. *
* *
*****************************************************************
* This program is provided by SimoTime Enterprises *
* Our e-mail address is: helpdesk@simotime.com *
* Also, visit our Web Site at http://www.simotime.com *
* *
*****************************************************************
*
*****************************************************************
* Source Member: CBLRMPC1.CBL
* Copy Files PASSPARS.CPY
* Calls to: SIMOPARS
*****************************************************************
*
* CBLRMPC1 - Call SIMOPARS to parse the buffer.
*
* CALLING PROTOCOL
* ----------------
* Use standard procedure to EXECUTE, RUN or ANIMATE.
*
* DESCRIPTION
* -----------
* This is a demonstration program to show how to call the
* COBOL Parsing Routine Routine.
*
* ************
* * CBLRMPJ1 *
* ********jcl*
* *
* *
* ************ ************
* * CBLRMPC1 *-----* CONSOLE *
* ********cbl* ******dsply*
* *
* *
* ************
* * SIMOPARS *
* ********cbl*
*
*****************************************************************
*
* MAINTENANCE
* -----------
* 1989/02/27 Simmons, Created program.
* 1997/03/17 Simmons, Updated for COBOL/2.
* 2002/09/23 Simmons, Added function to create a new field with
* leading spaces removed and embedded, multiple spaces
* reduced to a single space. The length of the output
* text string is also calculated.
*
*****************************************************************
*
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
*****************************************************************
* Data-structure for Title and Copyright...
* ------------------------------------------------------------
01 SIM-TITLE.
05 T1 pic X(11) value '* CBLRMPC1 '.
05 T2 pic X(34) value 'COBOL Parse a Data String '.
05 T3 pic X(10) value ' v03.12.02'.
05 T4 pic X(24) value ' http://www.simotime.com'.
01 SIM-COPYRIGHT.
05 C1 pic X(11) value '* CBLRMPC1 '.
05 C2 pic X(20) value 'Copyright 1987-2006 '.
05 C3 pic X(28) value ' SimoTime Enterprises, LLC '.
05 C4 pic X(20) value ' All Rights Reserved'.
01 SIM-THANKS-01.
05 C1 pic X(11) value '* CBLRMPC1 '.
05 C2 pic X(32) value 'Thank you for using this sample '.
05 C3 pic X(32) value 'by SimoTime Enterprises, LLC '.
05 C4 pic X(04) value ' '.
01 SIM-THANKS-02.
05 C1 pic X(11) value '* CBLRMPC1 '.
05 C2 pic X(32) value 'Please send comments or suggesti'.
05 C3 pic X(32) value 'ons to helpdesk@simotime.com '.
05 C4 pic X(04) value ' '.
*****************************************************************
* Buffer used for posting messages to the console.
* ------------------------------------------------------------
01 MESSAGE-BUFFER.
05 MESSAGE-HEADER pic X(11) value '* CBLRMPC1 '.
05 MESSAGE-TEXT pic X(245).
*****************************************************************
* Work fields used for testing call to SIMOPARS.
* ------------------------------------------------------------
01 IX-1 pic 9999 value 0.
01 IX-2 pic 9999 value 0.
* Simple parsing task...
01 TASK-01-DATA.
05 filler pic X(30) value 'This is a simple text string '.
* Used to parse using a slash (/) as the delimiter...
01 TASK-02-DATA.
05 filler pic X(10) value '01/24/2001'.
05 filler pic X(5) value is SPACES.
* Used to parse and handle the leading and/or embedded
* SPACE or multiple SPACES...
01 TASK-03-DATA.
05 filler pic X(07) value is SPACES.
05 filler pic X(30) value 'Text String with leading '.
05 filler pic X(30) value 'and multiple embedded '.
05 filler pic X(05) value is SPACES.
05 filler pic X(20) value 'SPACE characters '.
* Parse a larger data string...
01 TASK-04-DATA.
05 filler pic X(30) value ' The quick brown fox jumped '.
05 filler pic X(30) value 'over the lazy dogs back. Now '.
05 filler pic X(30) value 'is the time for all good men '.
05 filler pic X(30) value 'and women to come to the aid '.
05 filler pic X(30) value 'of the party. '.
05 filler pic X(30) value 'We have crossed the bridge and'.
05 filler pic X(30) value ' we are pushing the envelope'.
05 filler pic X(30) value ' to the top of the hill. '.
01 DATA-256 pic X(256) value SPACES.
01 WORK-256 pic X(256) value SPACES.
01 DATA-256-MESSAGE.
05 filler pic X(29) value 'Length of the output text is '.
05 DATA-256-LENGTH pic 9(4) value 0.
01 LAST-BYTE-BUFFER.
05 filler pic X(25) value 'Last significant byte is '.
05 LAST-BYTE pic 9(4) value 0.
COPY PASSPARS.
*****************************************************************
PROCEDURE DIVISION.
perform Z-POST-COPYRIGHT.
*! Do a simple parsing to identify the position and size
*! of words within an alphameric field.
perform SAMPLE-TASK-01.
*! Do a simple parsing to identify the position and size
*! of the mm, dd and ccyy within a date field.
perform SAMPLE-TASK-02.
*! Do the parsing to identify the position and size of the
*! words within an alphameric field. Also, create a new
*! field with the leading spaces removed and the embedded,
*! multiple spaces reduced to a single space.
perform SAMPLE-TASK-03.
*! Do the parsing to identify the position and size of the
*! words within an alphameric field. Also, create a new
*! field with the leading spaces removed and the embedded,
*! multiple spaces reduced to a single space. This is similar
*! to the preceding sample but parses a larger field.
perform SAMPLE-TASK-04.
perform Z-THANK-YOU.
GOBACK.
*****************************************************************
* Scan a data string and identify the starting position and
* length of each keyword in the string, The delimiter used for
* this task is a space character.
*****************************************************************
SAMPLE-TASK-01.
*! Display the start-of-task message.
move 'SAMPLE-TASK-01 is STARTING...' to MESSAGE-TEXT
perform Z-POST-MESSAGE
*! Display the input buffer.
move TASK-01-DATA to MESSAGE-TEXT
perform Z-POST-MESSAGE
*! Prepare control items for parsing.
move '0' to PRS-REQUEST
add 9 to ZERO giving PRS-STATUS
move ' ' to PRS-DELIMITER
move 'N' to PRS-TERMINATOR
move ' ' to PRS-TERMINATOR-BYTE
add 32 to ZERO giving PRS-TABLE-MAX
add 128 to ZERO giving PRS-BUFFER-SIZE
*! Move the data string to the parsing buffer and call
*! the parsing routine.
move TASK-01-DATA to PRS-BUFFER
call 'SIMOPARS' using PRS-PARAMETERS
*! Display the contents of the parsing tables.
add 1 to ZERO giving IX-1
perform until PRS-SIZE(IX-1) = 0
or PRS-POSITION(IX-1) = 0
or IX-1 > PRS-TABLE-MAX
perform Z-POST-TABLE-ITEM
add 1 to IX-1
end-perform
perform Z-POST-LAST-SIGNIFICANT-BYTE
*! Display the end-of-task message.
move 'SAMPLE-TASK-01 is COMPLETE...' to MESSAGE-TEXT
perform Z-POST-MESSAGE
exit.
*****************************************************************
* Scan a data string and identify the starting position and
* length of each keyword in the string, The delimiter used for
* this task is a slash (/) character.
* This example also uses a space character to terminate the
* parsing if the string.
*****************************************************************
SAMPLE-TASK-02.
*! Display the start-of-task message.
move 'SAMPLE-TASK-02 is STARTING...' to MESSAGE-TEXT
perform Z-POST-MESSAGE
*! Display the input buffer.
move TASK-02-DATA to MESSAGE-TEXT
perform Z-POST-MESSAGE
*! Prepare control items for parsing.
move '0' to PRS-REQUEST
add 9 to ZERO giving PRS-STATUS
move '/' to PRS-DELIMITER
move 'Y' to PRS-TERMINATOR
move ' ' to PRS-TERMINATOR-BYTE
add 32 to ZERO giving PRS-TABLE-MAX
add 128 to ZERO giving PRS-BUFFER-SIZE
*! Move the data string to the parsing buffer and call
*! the parsing routine.
move TASK-02-DATA to PRS-BUFFER
call 'SIMOPARS' using PRS-PARAMETERS
*! Display the contents of the parsing tables.
add 1 to ZERO giving IX-1
perform until PRS-SIZE(IX-1) = 0
or PRS-POSITION(IX-1) = 0
or IX-1 > PRS-TABLE-MAX
perform Z-POST-TABLE-ITEM
add 1 to IX-1
end-perform
perform Z-POST-LAST-SIGNIFICANT-BYTE
*! Display the end-of-task message.
move 'SAMPLE-TASK-02 is COMPLETE...' to MESSAGE-TEXT
perform Z-POST-MESSAGE
exit.
*****************************************************************
* Scan a data string and identify the starting position and
* length of each keyword in the string, The delimiter used for
* this task is a space character.
* The input data string in this task has leading and multiple,
* embedded spaces.
* The output data string has the leading spaces removed and the
* embedded multiple spaces are replaced with a single space.
*****************************************************************
SAMPLE-TASK-03.
*! Display the start-of-task message.
move 'SAMPLE-TASK-03 is STARTING...' to MESSAGE-TEXT
perform Z-POST-MESSAGE
*! Display the input buffer.
move 'Input with leading and embedded, multiple spaces...'
to MESSAGE-TEXT
perform Z-POST-MESSAGE
*! Display the input buffer.
move TASK-03-DATA to MESSAGE-TEXT
perform Z-POST-MESSAGE
*! Prepare control items for parsing.
move '0' to PRS-REQUEST
add 9 to ZERO giving PRS-STATUS
move ' ' to PRS-DELIMITER
move 'N' to PRS-TERMINATOR
move ' ' to PRS-TERMINATOR-BYTE
add 32 to ZERO giving PRS-TABLE-MAX
add 128 to ZERO giving PRS-BUFFER-SIZE
*! Move the data string to the parsing buffer and call
*! the parsing routine.
move TASK-03-DATA to PRS-BUFFER
call 'SIMOPARS' using PRS-PARAMETERS
*! Display the contents of the parsing tables.
add 1 to ZERO giving IX-1
perform until PRS-SIZE(IX-1) = 0
or PRS-POSITION(IX-1) = 0
or IX-1 > PRS-TABLE-MAX
perform Z-POST-TABLE-ITEM
add 1 to IX-1
end-perform
perform Z-POST-LAST-SIGNIFICANT-BYTE
*! Create and display the output buffer.
move 'Output w/o leading and embedded, multiple spaces...'
to MESSAGE-TEXT
perform Z-POST-MESSAGE
move TASK-03-DATA to WORK-256
perform SAMPLE-TASK-99
move DATA-256-MESSAGE to MESSAGE-TEXT
perform Z-POST-MESSAGE
move DATA-256 to MESSAGE-TEXT
perform Z-POST-MESSAGE
*! Display the end-of-task message.
move 'SAMPLE-TASK-03 is COMPLETE...' to MESSAGE-TEXT
perform Z-POST-MESSAGE
exit.
*****************************************************************
* Scan a data string and identify the starting position and
* length of each keyword in the string, The delimiter used for
* this task is a space character.
* The input data string in this task has leading and multiple,
* embedded spaces.
* The output data string has the leading spaces removed and the
* embedded multiple spaces are replaced with a single space.
*****************************************************************
SAMPLE-TASK-04.
*! Display the start-of-task message.
move 'SAMPLE-TASK-04 is STARTING...' to MESSAGE-TEXT
perform Z-POST-MESSAGE
*! Display the input buffer.
move 'Input with leading and embedded, multiple spaces...'
to MESSAGE-TEXT
perform Z-POST-MESSAGE
*! Display the input buffer.
move TASK-04-DATA to MESSAGE-TEXT
perform Z-POST-MESSAGE
*! Prepare control items for parsing.
move '0' to PRS-REQUEST
add 9 to ZERO giving PRS-STATUS
move ' ' to PRS-DELIMITER
move 'N' to PRS-TERMINATOR
move ' ' to PRS-TERMINATOR-BYTE
add 64 to ZERO giving PRS-TABLE-MAX
add 240 to ZERO giving PRS-BUFFER-SIZE
*! Move the data string to the parsing buffer and call
*! the parsing routine.
move TASK-04-DATA to PRS-BUFFER
call 'SIMOPARS' using PRS-PARAMETERS
*! Display the contents of the parsing tables.
add 1 to ZERO giving IX-1
perform until PRS-SIZE(IX-1) = 0
or PRS-POSITION(IX-1) = 0
or IX-1 > PRS-TABLE-MAX
perform Z-POST-TABLE-ITEM
add 1 to IX-1
end-perform
perform Z-POST-LAST-SIGNIFICANT-BYTE
move TASK-04-DATA to MESSAGE-TEXT
perform Z-POST-MESSAGE
*! Create and display the output buffer.
move 'Output w/o leading and embedded, multiple spaces...'
to MESSAGE-TEXT
perform Z-POST-MESSAGE
move SPACES to WORK-256
move TASK-04-DATA to WORK-256
perform SAMPLE-TASK-99
move DATA-256-MESSAGE to MESSAGE-TEXT
perform Z-POST-MESSAGE
move DATA-256-MESSAGE to MESSAGE-TEXT
move DATA-256 to MESSAGE-TEXT
perform Z-POST-MESSAGE
*! Display the end-of-task message.
move 'SAMPLE-TASK-04 is COMPLETE...' to MESSAGE-TEXT
perform Z-POST-MESSAGE
exit.
*****************************************************************
* Build a new string with a single space between words and
* remove the leading spaces.
*****************************************************************
SAMPLE-TASK-99.
add 1 to ZERO giving IX-1
add 1 to ZERO giving IX-2
move SPACES to DATA-256
perform until PRS-SIZE(IX-1) = 0
or PRS-POSITION(IX-1) = 0
or IX-1 > PRS-TABLE-MAX
move WORK-256(PRS-POSITION(IX-1):PRS-SIZE(IX-1))
to DATA-256(IX-2:PRS-SIZE(IX-1))
add PRS-SIZE(IX-1) to IX-2
add 1 to IX-2
add 1 to IX-1
end-perform
if IX-2 > 1
subtract 2 from IX-2 giving DATA-256-LENGTH
else
add 1 to ZERO giving DATA-256-LENGTH
end-if
exit.
*****************************************************************
Z-POST-COPYRIGHT.
display SIM-TITLE upon console
display SIM-COPYRIGHT upon console
exit.
Z-POST-LAST-SIGNIFICANT-BYTE.
add PRS-LAST-SIG-BYTE to ZERO giving LAST-BYTE
move LAST-BYTE-BUFFER to MESSAGE-TEXT
perform Z-POST-MESSAGE
exit.
Z-POST-MESSAGE.
if MESSAGE-BUFFER(80:177) = SPACES
display MESSAGE-BUFFER(1:79) upon console
move SPACES to MESSAGE-TEXT
else
display MESSAGE-BUFFER upon console
move SPACES to MESSAGE-TEXT
end-if
exit.
Z-POST-TABLE-ITEM.
move 'Item-nnnn, Position-nnnn, Size-nnnn, Parameter - '
to MESSAGE-TEXT(1:49)
move IX-1 to MESSAGE-TEXT(6:4)
move PRS-POSITION(IX-1) to MESSAGE-TEXT(21:4)
move PRS-SIZE(IX-1) to MESSAGE-TEXT(32:4)
if PRS-SIZE(IX-1) < 20
move PRS-BUFFER(PRS-POSITION(IX-1):PRS-SIZE(IX-1))
to MESSAGE-TEXT(50:PRS-SIZE(IX-1))
else
move PRS-BUFFER(PRS-POSITION(IX-1):19)
to MESSAGE-TEXT(50:19)
end-if
perform Z-POST-MESSAGE
exit.
*****************************************************************
Z-THANK-YOU.
display SIM-THANKS-01 upon console
display SIM-THANKS-02 upon console
exit.
*****************************************************************
* This example is provided by SimoTime Enterprises *
* Our e-mail address is: helpdesk@simotime.com *
* Also, visit our Web Site at http://www.simotime.com *
*****************************************************************
This program (SIMOPARS.CBL) was written to provide parsing capability of a data string. It uses "Reference Modification" to scan the data string.
This program provide very little visual information when it is executed on the mainframe. The real value of this program is observed when it is animated using Mainframe Express provided by Micro Focus. It is possible to watch the actual execution of each individual instruction and to immediately see the results.
IDENTIFICATION DIVISION.
PROGRAM-ID. SIMOPARS.
AUTHOR. SIMOTIME ENTERPRISES.
*****************************************************************
* Copyright (C) 1987-2006 SimoTime Enterprises, LLC. *
* *
* All rights reserved. Unpublished, all rights reserved under *
* copyright law and international treaty. Use of a copyright *
* notice is precautionary only and does not imply publication *
* or disclosure. *
* *
* Permission to use, copy, modify and distribute this software *
* for any non-commercial purpose and without fee is hereby *
* granted, provided the SimoTime copyright notice appear on all *
* copies of the software. The SimoTime name or Logo may not be *
* used in any advertising or publicity pertaining to the use *
* of the software without the written permission of SimoTime *
* Enterprises.This software contains confidential information *
* *
* Permission to use, copy, modify and distribute this software *
* for any commercial purpose requires a fee to be paid to *
* Simotime Enterprises. Once the fee is received by SimoTime *
* the latest version of the software will be delivered and a *
* license will be granted for use within an enterprise, *
* provided the SimoTime copyright notice appear on all copies *
* of the software. The SimoTime name or Logo may not be used *
* in any advertising or publicity pertaining to the use of the *
* software without the written permission of SimoTime *
* Enterprises. *
* *
* SimoTime Enterprises makes no warranty or representations *
* about the suitability of the software for any purpose. It is *
* provided "AS IS" without any express or implied warranty, *
* including the implied warranties of merchantability, fitness *
* for a particular purpose and non-infringement. SimoTime *
* Enterprises shall not be liable for any direct, indirect, *
* special or consequential damages resulting from the loss of *
* use, data or projects, whether in an action of contract or *
* tort, arising out of or in connection with the use or *
* performance of this software *
* *
* SimoTime Enterprises *
* 15 Carnoustie Drive *
* Novato, CA 94949-5849 *
* 415.883.6565 *
* *
* RESTRICTED RIGHTS LEGEND *
* Use, duplication, or disclosure by the Government is subject *
* to restrictions as set forth in subparagraph (c)(1)(ii) of *
* the Rights in Technical Data and Computer Software clause at *
* DFARS 52.227-7013 or subparagraphs (c)(1) and (2) of *
* Commercial Computer Software - Restricted Rights at 48 *
* CFR 52.227-19, as applicable. Contact SimoTime Enterprises, *
* 15 Carnoustie Drive, Novato, CA 94949-5849. *
* *
*****************************************************************
* This program is provided by SimoTime Enterprises *
* Our e-mail address is: helpdesk@simotime.com *
* Also, visit our Web Site at http://www.simotime.com *
* *
*****************************************************************
*
*****************************************************************
* Source Member: SIMOPARS.CBL
* Copy Files PASSPARS.CPY
*****************************************************************
*
* SIMOPARS - Parse Buffer defined in pass area.
*
* CALLING PROTOCOL
* ----------------
* call 'SIMOPARS' using PRS-PARAMETERS
*
* 01 PRS-PARAMETERS.
* 05 PRS-REQUEST PIC X VALUE '0'.
* 05 PRS-STATUS PIC 9(4).
* 05 PRS-DELIMITER PIC X VALUE SPACE.
* 05 PRS-KEEP-NULL-FIELDS PIC X VALUE 'N'.
* 05 PRS-SUSPEND PIC X VALUE 'N'.
* 05 PRS-SUSPEND-BYTE PIC X VALUE SPACE.
* 05 PRS-TERMINATOR PIC X VALUE 'N'.
* 05 PRS-TERMINATOR-BYTE PIC X VALUE SPACE.
* 05 PRS-BUFFER-SIZE PIC 9(4) VALUE 1024.
* 05 PRS-BUFFER PIC X(1024).
* 05 PRS-TABLE-MAX PIC 9(4) VALUE 64.
* 05 PRS-NUMBER-OF-ITEMS PIC 9(4) VALUE 0.
* 05 PRS-LAST-SIG-BYTE PIC 9(4) VALUE 0.
* 05 PRS-POSITION OCCURS 64 TIMES
* PIC 9(4) VALUE 0.
* 05 PRS-SIZE OCCURS 64 TIMES
*
* This routine uses reference modification to identify the
* position of the first significant character after the
* delimiter character. This approach compensates for multiple
* leading or embedded delimiter characters. The string function
* of COBOL does not handle leading spaces.
*
* For example, if the delimiter character is a space then
* leading spaces will be ignored and multiple, embedded spaces
* will be treated as a single space.
*
* MAINTENANCE
* -----------
* 1998/01/02 Simmons, CREATED PROGRAM.
* 1998/01/02 Simmons, No changes to date...
*
*****************************************************************
*
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
*****************************************************************
* Data-structure for Program use... *
*****************************************************************
01 I-PTR pic 9(4) value 0.
01 O-PTR pic 9(4) value 0.
01 B-COUNT pic 9(4) value 0.
01 SUSPEND-SWITCH pic X value 'N'.
*****************************************************************
LINKAGE SECTION.
COPY PASSPARS.
*****************************************************************
PROCEDURE DIVISION using PRS-PARAMETERS.
add 8 to ZERO giving RETURN-CODE
add 9 to ZERO giving PRS-STATUS
subtract 1 from 1 giving PRS-NUMBER-OF-ITEMS
evaluate PRS-REQUEST
when '0' perform PARSE-BUFFER
when '1' perform INITIALIZE-TABLE-ELEMENTS
when OTHER add 12 to ZERO giving PRS-STATUS
end-evaluate
if PRS-STATUS = 9
subtract PRS-STATUS from PRS-STATUS
end-if
GOBACK.
*****************************************************************
INITIALIZE-TABLE-ELEMENTS.
move 1 to I-PTR
move 1 to O-PTR
perform until O-PTR > PRS-TABLE-MAX
move 0 to PRS-POSITION(O-PTR)
move 0 to PRS-SIZE(O-PTR)
add 1 to O-PTR
end-perform
subtract RETURN-CODE from RETURN-CODE
exit.
*****************************************************************
PARSE-BUFFER.
*! Initialize Offset/Length tables to zero (0).
perform INITIALIZE-TABLE-ELEMENTS
add 1 to ZERO giving PRS-LAST-SIG-BYTE
subtract PRS-LAST-SIG-BYTE from PRS-LAST-SIG-BYTE
*! Parse the Buffer.
add 1 to ZERO giving O-PTR
perform until I-PTR > PRS-BUFFER-SIZE
if PRS-SUSPEND = 'Y'
and PRS-BUFFER(I-PTR:1) = PRS-SUSPEND-BYTE
if SUSPEND-SWITCH = 'Y'
move 'N' to SUSPEND-SWITCH
else
move 'Y' to SUSPEND-SWITCH
end-if
else
if PRS-BUFFER(I-PTR:1) = PRS-DELIMITER
and SUSPEND-SWITCH = 'N'
add 1 to B-COUNT
if PRS-KEEP-NULL-FIELDS = 'Y'
or B-COUNT = 1
and PRS-SIZE(O-PTR) > 0
if O-PTR < PRS-TABLE-MAX
add 1 to O-PTR
add 1 to PRS-NUMBER-OF-ITEMS
else
move PRS-BUFFER-SIZE to I-PTR
end-if
end-if
else
subtract B-COUNT from B-COUNT
add 1 to PRS-SIZE(O-PTR)
if PRS-SIZE(O-PTR) = 1
move I-PTR to PRS-POSITION(O-PTR)
end-if
end-if
end-if
add 1 to I-PTR
if PRS-TERMINATOR = 'Y'
and I-PTR not > PRS-BUFFER-SIZE
and PRS-BUFFER(I-PTR:1) = PRS-TERMINATOR-BYTE
if PRS-SIZE(O-PTR) > 0
add 1 to PRS-NUMBER-OF-ITEMS
end-if
add PRS-BUFFER-SIZE to 1 giving I-PTR
end-if
end-perform
if PRS-POSITION(O-PTR) = 0
subtract 1 from O-PTR
end-if
if PRS-NUMBER-OF-ITEMS > 0
add PRS-POSITION(PRS-NUMBER-OF-ITEMS)
to PRS-SIZE(PRS-NUMBER-OF-ITEMS)
giving PRS-LAST-SIG-BYTE
subtract 1 from PRS-LAST-SIG-BYTE
end-if
subtract RETURN-CODE from RETURN-CODE
exit.
*****************************************************************
* This example is provided by SimoTime Enterprises *
* Our e-mail address is: helpdesk@simotime.com *
* Also, visit our Web Site at http://www.simotime.com *
*****************************************************************
The following is the copy file (PASSPARS.CPY) used for the pass area when calling the parsing routine.
*****************************************************************
* Data Structure used for calling SIMOPARS. *
*****************************************************************
* Copyright (C) 1987-2006 SimoTime Enterprises *
* All Rights Reserved *
*****************************************************************
* Provided by SimoTime Enterprises *
* Our e-mail address is: helpdesk@simotime.com *
* Also, visit our Web Site at http://www.simotime.com *
*****************************************************************
* *
* PRS-REQUEST Type of request, must be zero (0) for *
* parsing. This item is provided by the *
* calling program and is not modified *
* by the parsing routine. *
* PRS-STATUS This is an indicator as to the success *
* or failure of the request. A value of *
* zero (0000) indicates the routine was *
* successful. A non-zero value indicates *
* a failure. *
* *
* ------------------------------------------------------------- *
* The following fields are provided by the calling routine. *
* *
* PRS-DELIMITER This is a one byte character that is *
* used as a delimiter. This item is *
* provided by the calling program and *
* is not modified by the parsing routine. *
* *
* PRS-KEEP-NULL-FIELDS This must be a Yes (Y) or No (N) value *
* (Y) will indicate a Yes, keep null field *
* (N) will indicate a No and null fields *
* will be dropped. *
* PRS-SUSPEND This must be a Yes (Y) or No (N) value *
* to indicate that the suspend/resume *
* function of checking for a delimiter *
* is active. *
* PRS-SUSPEND-BYTE If the previous parameter is (Y) this *
* byte will be used to suspend or resume *
* the testing for a delimiter charcter. *
* PRS-TERMINATOR This must be a Yes (Y) or No (N) value *
* to indicate a character that will be *
* used for termination of the parsing. *
* If (Y) then the next parameter is used *
* as a termination character. If (N) the *
* buffer will be parsed according to the *
* size of the buffer or maximum number *
* of table entries. This item is provided *
* by the calling program and is not *
* modified by the parsing routine. *
* PRS-TERMINATOR-BYTE If the previous parameter is (Y) this *
* byte will be used to terminate the *
* parsing when it is encountered in the *
* data string. This item is provided by *
* the calling program and is not modified *
* by the parsing routine. *
* *
* PRS-BUFFER-SIZE The size of the data string to be *
* parsed and must be a value of 1-1024. *
* This item is provided by the calling *
* program and is not modified by the *
* parsing routine. *
* PRS-BUFFER This is the data string or field that *
* will be parsed. This item is provided *
* by the calling program and is not *
* modified by the parsing routine. *
* *
* PRS-TABLE-MAX The maximum number of table entries *
* available for the POSITION/SIZE values *
* of the words in the input buffer. *
* This item is provided by the calling *
* program and is not modified by the *
* parsing routine. *
* *
* ------------------------------------------------------------- *
* The following fields are updated by the parsing routine. *
* *
* PRS-NUMBER-OF-ITEMS This is the number of keywords found *
* in the input data string. This item is *
* provided by the parsing routine. *
* *
* PRS-LAST-SIG-BYTE This is the position of the last *
* significant, non-delimiter byte. If the *
* PRS-TERMINATOR is "Y" this will be the *
* last significant byte before the *
* terminator byte. *
* *
* PRS-POSITION This is a table of 64 elements of *
* 4-bytes. The value is either zero (0) *
* or the position of a keyword within the *
* input data string. The first position *
* within the buffer is one (1). *
* This item is provided by the parsing *
* routine. *
* PRS-SIZE This is a table of 64 elements of *
* 4-bytes. The value is either zero (0) *
* or the size of a keyword within the *
* input data string. This item is provided *
* by the parsing routine. *
* *
* ------------------------------------------------------------- *
* NOTE... The table element number is the number *
* of the word within the data buffer. *
* The data buffer is PRS-BUFFER. *
* For example, PRS-POSITION(1) would point *
* to the position of the first character *
* of the first word in the data buffer *
* PRS-SIZE(1) would specify the number of *
* characters in the first word. *
* *
*****************************************************************
01 PRS-PARAMETERS.
05 PRS-REQUEST PIC X VALUE '0'.
05 PRS-STATUS PIC 9(4).
*! Provided by the calling program...
05 PRS-DELIMITER PIC X VALUE SPACE.
05 PRS-KEEP-NULL-FIELDS PIC X VALUE 'N'.
05 PRS-SUSPEND PIC X VALUE 'N'.
05 PRS-SUSPEND-BYTE PIC X VALUE SPACE.
05 PRS-TERMINATOR PIC X VALUE 'N'.
05 PRS-TERMINATOR-BYTE PIC X VALUE SPACE.
05 PRS-BUFFER-SIZE PIC 9(4) VALUE 1024.
05 PRS-BUFFER PIC X(1024).
05 PRS-TABLE-MAX PIC 9(4) VALUE 64.
*! Updated by the parsing routine...
05 PRS-NUMBER-OF-ITEMS PIC 9(4) VALUE 0.
05 PRS-LAST-SIG-BYTE PIC 9(4) VALUE 0.
05 PRS-POSITION OCCURS 64 TIMES
PIC 9(4) VALUE 0.
05 PRS-SIZE OCCURS 64 TIMES
PIC 9(4) VALUE 0.
*! PASSPARS - End-of-Copy File...
The purpose of this document is to assist as a tutorial for new programmers or as a quick reference for experienced programmers. These sample programs are made available on an "as-is" basis and may be downloaded, copied and modified for specific situations as long as the copyright information is not removed or changed. As always, it is the programmer's responsibility to thoroughly test all programs.
Permission to use, copy, modify and distribute this software, documentation or training material for any purpose requires a fee to be paid to SimoTIme Enterprises. Once the fee is received by SimoTime the latest version of the software, documentation or training material will be delivered and a license will be granted for use within an enterprise, provided the SimoTime copyright notice appear on all copies of the software. The SimoTime name or Logo may not be used in any advertising or publicity pertaining to the use of the software without the written permission of SimoTime Enterprises.
SimoTime Enterprises makes no warranty or representations about the suitability of the software, documentation or learning material for any purpose. It is provided "AS IS" without any express or implied warranty, including the implied warranties of merchantability, fitness for a particular purpose and non-infringement. SimoTime Enterprises shall not be liable for any direct, indirect, special or consequential damages resulting from the loss of use, data or projects, whether in an action of contract or tort, arising out of or in connection with the use or performance of this software, documentation or training material.
If you have any questions, suggestions or comments please call or send an e-mail to: helpdesk@simotime.com
You may download this example at http://www.simotime.com/sim4dzip.htm#COBOLParsing as a Z-Pack. The Z-Packs provide individual programming examples, documentation and test data files in a single package. The Z-Packs are usually in zip format to reduce the amount of time to download.
Please view the complete list of SimoTime Z-Pack Examples at http://www.simotime.com/sim4dzip.htm.
Note: You must be attached to the Internet to download a Z-Pack or view the list.
An example of editing a street address is provided at http://www.simotime.com/cblstr01.htm. This example will scan an input street address and create a new street address with upper-lower case formatting, allow word substitution and identify Post Office Box addresses.
Check out The COBOL Connection for more examples of mainframe COBOL coding techniques and sample code.
Check out The SimoTime Library for a wide range of topics for Programmers, Project Managers and Software Developers.
To review all the information available on this site start at The SimoTime Home Page .
If you have any questions, suggestions or comments please call or send an e-mail to: helpdesk@simotime.com
Founded in 1987, SimoTime Enterprises is a privately owned, Limited Liability Corporation located in Novato, California. We specialize in the creation and deployment of business applications using new or existing technologies and services. We have a team of individuals that understand the broad range of technologies being used in today's environments. This includes the smallest thin client using the Internet and the very large mainframe systems. There is more to making the Internet work for your company's business than just having a nice looking WEB site. It is about combining the latest technologies and existing technologies with practical business experience. It's about the business of doing business and looking good in the process. Quite often, to reach larger markets or provide a higher level of service to existing customers it requires the newer Internet technologies to work in a complementary manner with existing corporate mainframe systems. Whether you want to use the Internet to expand into new market segments or as a delivery vehicle for existing business functions simply give us a call or check the web site at http://www.simotime.com
| Return-to-Top |
| Copyright © 1987-2008 SimoTime Enterprises, LLC All Rights Reserved |
| When technology complements business |
| http://www.simotime.com |