SUBSTRING for a String Literal in COBOL

50,663

Solution 1

You can accomplish what you are trying to do by type-laundering the literal through a function. You can then substring, or reference modify, the output of the function. Consider that calling reverse twice on the same data returns the original data.

Move function reverse                           
 ( function reverse( 
      'abcdefg' 
   )
 ) (3:1) to text-out

The above will result in a 'c' being moved to text-out.

Solution 2

Of course, the example code in your question does not make any sense, as why would you write "HELLO"(1:3) when you could just write "HEL".

So you must be wanting to use a variable (or 2) in the reference modifier field(s).

If you are wanting to get the first 'N' characters of the literal, you can do this by using the reference modifier on the destination item. For example, if you compile and run the following program:

   IDENTIFICATION DIVISION.
   PROGRAM-ID. HELLO.
   DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 LEN          PIC 99 VALUE 8.
   01 SUB-STR      PIC X(80).
   PROCEDURE DIVISION.
       MOVE "HELLO WORLD" TO SUB-STR(1:LEN).
       DISPLAY SUB-STR.
       STOP RUN.

You get the resulting output:

HELLO WO

Unfortunately this method only works if you want the first 'N' characters of the literal string.

Also, the destination string must be empty before you start. In the above program, if you changed the definition of SUB-STR to be:

01 SUB-STR      PIC X(80) VALUE "BLAH BLAH BLAH".

Then the result of running the program becomes:

HELLO WOH BLAH

Solution 3

Put the "literal" into a field, like a constant.

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 LITERAL-HELLO PIC X(5) VALUE 'HELLO'.
PROCEDURE DIVISION.
    DISPLAY LITERAL-HELLO(1:3).
    STOP RUN.
Share:
50,663
mhshams
Author by

mhshams

Software Architect / Java Developer.

Updated on May 23, 2020

Comments

  • mhshams
    mhshams almost 4 years

    Is there anyway to get a SUBSTRING of string literal in COBOL without using a temporary variable?

    Let's say in the following code:

    MOVE "HELLO" TO MY-VAR.
    MOVE MY-VAR(1:3) TO SUB-STR.
    

    Is there any way to do the same thing, but without MY-VAR?

    EDIT: I did tried following code, but it's failed.

    MOVE "HELLO"(1:3) TO SUB-STR   * COMPILE ERROR
    
    • NealB
      NealB over 12 years
      As stated, your queustion doesn't make a lot of sense to me. Can you explain in a bit more detail what you are trying to accomplish? There might be a completely different approach to achieving the results you are looking for.
    • Raja Reddy
      Raja Reddy over 12 years
      True! I'm curious as well. Even though you had got the solution, can you spare sometime to get us the requirement and the way it was resolved. That helps!
  • Joe Zitzelberger
    Joe Zitzelberger over 12 years
    Having said that, why would you want to reference modify a literal. All values are known at compile time...
  • mhshams
    mhshams over 12 years
    thanks for the solution. about your question: it's a long story but in short: we are converting an script (written in other language) to cobol code. toString(<String>) is a valid function in the script language and user can pass either a string variable or a string literal. I know it's not smart to pass stirng literal (as you mentioned) but end user (possibly) can do it in the script. so we need to handle it too. thanks again.
  • mhshams
    mhshams about 12 years
    as i mentioned in my question, I don't want to define a new variable (LITERAL-HELLO in your answer). actually for some reasons I can't define new variable.