How do I concatenate three fields into one and remove the trailing blanks?

Document ID : KB000056203
Last Modified Date : 14/02/2018
Show Technical Document Details

Here are instructions on concatenating three fields into one and removing the trailing blanks. A sample program that accomplishes the task is included.

* WORKING STORAGE
*****************************************************************
*****************************************************************
*  CONCATENATE 3 FIELDS INTO ONE FIELD AND REMOVE THE TRAILING BLANKS
*  E.G. PERSONNEL FILE WITH FIRST NAME, MIDDLE INITIAL, AND LAST NAME
*   
*****************************************************************
*
DEFINE FIRST-NAME         W   15  A  VALUE 'JOHN           '
DEFINE F-ARRAY   FIRST-NAME    1  A  OCCURS 15
*
DEFINE MIDDLE-INITIAL     W    1  A  VALUE 'Q'
*
DEFINE LAST-NAME          W   20  A  VALUE 'SMITH               '
DEFINE L-ARRAY    LAST-NAME    1  A  OCCURS 20
*
**********************************************************
*                                                        *
*  THE FIRST NAME AND LAST NAME ARRAYS USE SUBSCRIPTING  *
*                                                        *
**********************************************************
DEFINE NAME-SUB           W    2  N
*
**********************************************************
*                                                        *
*  THE ONE FIELD ARRAY USES INDEXING                     *
*                                                        *
**********************************************************
*
DEFINE ONE-FIELD          W   39  A
DEFINE ONE-ARRAY   ONE-FIELD   1  A  OCCURS 39 INDEX ONE-IX
DEFINE SEVEN-ARRAY ONE-FIELD   7  A  OCCURS 1  INDEX ONE-IX
DEFINE EIGHT-ARRAY ONE-FIELD   8  A  OCCURS 1  INDEX ONE-IX
*
JOB INPUT NULL
    ONE-FIELD = ' '
    NAME-SUB = 1
    ONE-IX = 0
*
    DO UNTIL NAME-SUB EQ 16
       IF F-ARRAY (NAME-SUB) NE ' '
          ONE-ARRAY = F-ARRAY (NAME-SUB)
          ONE-IX = ONE-IX + 1
          NAME-SUB = NAME-SUB + 1
       ELSE
          NAME-SUB = 16
       END-IF
    END-DO
*
    ONE-IX = ONE-IX + 1
    ONE-ARRAY = MIDDLE-INITIAL
    ONE-IX = ONE-IX + 1
    ONE-ARRAY = '.'
    ONE-IX = ONE-IX + 2
*
    NAME-SUB = 1
*
    DO UNTIL NAME-SUB EQ 21
       IF L-ARRAY (NAME-SUB) NE ' '
          ONE-ARRAY = L-ARRAY (NAME-SUB)
          ONE-IX = ONE-IX + 1
          NAME-SUB = NAME-SUB + 1
       ELSE
          NAME-SUB = 21
       END-IF
   END-DO
*
* AT THIS TIME ONE-FIELD CONTAINS FIRST MIDDLE AND LAST NAME

   DISPLAY '***************************************'
   DISPLAY 'ONE FIELD = ' ONE-FIELD
   DISPLAY '***************************************'

*
STOP
*

PRODUCES THE FOLLOWING OUTPUT:

***************************************
ONE FIELD = JOHN Q. SMITH
***************************************