COBOL alternative to BASIC's MID and how to concat strings?

1.9k views Asked by At

I'm looking for the COBOL alternative of Visual Basic's MID Function. The thing I need to do is take from 8 strings the first 5 letters and concatenate them.

I'm using Fujitsu COBOL.

Many thanks,

Yvan

4

There are 4 answers

0
NealB On BEST ANSWER

Paxdiablo has given a couple of valid ways to do it. Another way would be to use reference modification in addition to the STRING verb. Complete program example follows:

   IDENTIFICATION DIVISION.                                      
   PROGRAM-ID. EXAMPLE9.                                         
   DATA DIVISION.                                                
   WORKING-STORAGE SECTION.                                      
   01  STRINGS.                                                  
       05 STRING1    PIC X(10) VALUE 'AAAAAAAAAA'.               
       05 STRING2    PIC X(10) VALUE 'BBBBBBBBBB'.               
       05 STRING3    PIC X(10) VALUE 'CCCCCCCCCC'.               
       05 STRING4    PIC X(10) VALUE 'DDDDDDDDDD'.               
       05 STRING5    PIC X(10) VALUE 'EEEEEEEEEE'.               
       05 STRING6    PIC X(10) VALUE 'FFFFFFFFFF'.               
       05 STRING7    PIC X(10) VALUE 'GGGGGGGGGG'.               
       05 STRING8    PIC X(10) VALUE 'HHHHHHHHHH'.               
       05 STRING-OUT PIC X(40) VALUE SPACES.                     
   PROCEDURE DIVISION.

       STRING STRING1(1:5) STRING2(1:5) STRING3(1:5) STRING4(1:5)
              STRING5(1:5) STRING6(1:5) STRING7(1:5) STRING8(1:5) 
              DELIMITED BY SIZE                                   
         INTO STRING-OUT

       DISPLAY STRING-OUT                                         
       GOBACK.                                                    

This cuts down on the verbosity quite a bit and captures the concatenation in a single statement. Best advice is to read up on the STRING verb. There are a number of innovative ways it can be used.

COBOL does not provide an exact analogue to the BASIC MID statement. You can accomplish similar operations by using some combination of STRING, UNSTRING, INSPECT and reference modification. An example of a reference modification is: SOME-VARIABLE-NAME(1:5) - the 1:5 bit specifies a substring of SOME-VARIABLE-NAME starting with the first character for a length of 5 characters. The modifiers may themselves be numeric variables. The STRING and UNSTRING verbs provide a number of features that can be quite powerful.

In general though, COBOL is not particularly good at string manipulation (some might say its not particularly good at anything - but I would disagree with that statement).

0
Bill Woodger On
WORKING STORAGE SECTION.
01  NORMAL-STRING-A    PIC X(80)
01  NORMAL-STRING-B    PIC X(80)
01  NORMAL-STRING-C    PIC X(80)
01  NORMAL-STRING-D    PIC X(80)
01  NORMAL-STRING-E    PIC X(80)
01  TWENTY-FIVE-A.
    05  FIVE-A         PIC X(5).
    05  FIVE-B         PIC X(5).
    05  FIVE-C         PIC X(5).
    05  FIVE-D         PIC X(5).
    05  FIVE-E         PIC X(5).
01  TWENTY-FIVE-B REDEFINES TWENTY-FIVE-A PIC X(25).

PROCEDURE DIVISION.
    MOVE NORMAL-STRING-A TO FIVE-A
    MOVE NORMAL-STRING-B TO FIVE-B
    MOVE NORMAL-STRING-C TO FIVE-C
    MOVE NORMAL-STRING-D TO FIVE-D
    MOVE NORMAL-STRING-E TO FIVE-E
0
paxdiablo On

I think it goes something like this:

WORKING STORAGE SECTION.
    01  NORMAL-STRING-A    PIC X(80)
    01  NORMAL-STRING-B    PIC X(80)
    01  NORMAL-STRING-C    PIC X(80)
    01  NORMAL-STRING-D    PIC X(80)
    01  NORMAL-STRING-E    PIC X(80)
    01  SUB-STRING.
        05  FIVE           PIC X(5)
        05  REST           PIC X(75)
    01  TWENTY-FIVE-A.
        05  FIVE-A         PIC X(5).
        05  FIVE-B         PIC X(5).
        05  FIVE-C         PIC X(5).
        05  FIVE-D         PIC X(5).
        05  FIVE-E         PIC X(5).
    01  TWENTY-FIVE-B      PIC X(25).

PROCEDURE DIVISION.
    MOVE NORMAL-STRING-A TO SUB-STRING.
    MOVE FIVE TO FIVE-A.

    MOVE NORMAL-STRING-B TO SUB-STRING.
    MOVE FIVE TO FIVE-B.

    MOVE NORMAL-STRING-C TO SUB-STRING.
    MOVE FIVE TO FIVE-C.

    MOVE NORMAL-STRING-D TO SUB-STRING.
    MOVE FIVE TO FIVE-D.

    MOVE NORMAL-STRING-E TO SUB-STRING.
    MOVE FIVE TO FIVE-E.

    MOVE TWENTY-FIVE-A TO TWENTY-FIVE-B.

Then, your string is in TWENTY-FIVE-B.

You know, I can't imagine why people thought COBOL was verbose :-)


On a more serious note, I think you can do something along these lines to achieve the same result (you may have to fiddle with the start and length parameters, it's been a while since I did any serious COBOL):

WORKING STORAGE SECTION.
    01  STRING-A           PIC X(80)
    01  STRING-B           PIC X(80)
    01  STRING-C           PIC X(80)
    01  STRING-D           PIC X(80)
    01  STRING-E           PIC X(80)
    01  TWENTY-FIVE        PIC X(25).

PROCEDURE DIVISION.
    MOVE STRING-A(1:5) TO TWENTY-FIVE( 1:5).
    MOVE STRING-B(1:5) TO TWENTY-FIVE( 6:5).
    MOVE STRING-C(1:5) TO TWENTY-FIVE(11:5).
    MOVE STRING-D(1:5) TO TWENTY-FIVE(16:5).
    MOVE STRING-E(1:5) TO TWENTY-FIVE(21:5).
0
gimel On

This substring examples page shows a few variations. An example:

*   Task3 suffix(xStr,Length)
*   Extract the last Length number of chars from a string
*   Solution - use reference modification with start of substring
*   defined as the FullStringLength - SubStringLength + 1
*   In this example we get the last 13 characters.
MOVE 13 TO StrLength
DISPLAY "Task3 = " xStr2((StrSize - StrLength) + 1:StrLength)