Comparing 2 PIC X strings of characters in COBOL

Kofuku-san picture Kofuku-san · Aug 31, 2014 · Viewed 9k times · Source

I'm really new to COBOL and I would like to ask a question. What if I have 2 PIC of characters and I would like to know if they are the same string

   77 name1 PIC x(20).
   77 name2 PIC x(20).

   PROCEDURE DIVISION.
      DISPLAY "Type the first name: " WITH NO ADVANCING
      ACCEPT name1.
      DISPLAY "Type the second name: " WITH NO ADVANCING
      ACCEPT name2.

I tried to search on google and found the Search method. But I can't really understand it and I think it will not work on my case since I'm not using a table.

Answer

Jeff Puckett picture Jeff Puckett · May 13, 2016

Just to build on @Dai's answer, I'm running on z/OS and every other comparison operator listed on Page 6-8 in that reference worked for me except for the EQUALS operator as expressed in @Dai's answer.

works

IS EQUAL TO

IF name1 IS EQUAL TO name2
    DISPLAY "Names are the same"
ELSE
    DISPLAY "Names are not the same"
END-IF.

works

IS =

IF name1 IS = name2
    DISPLAY "Names are the same"
ELSE
    DISPLAY "Names are not the same"
END-IF.

does not work

EQUALS

IF name1 EQUALS name2
    DISPLAY "Names are the same"
ELSE
    DISPLAY "Names are not the same"
END-IF.

results in this JCL condition code 12 compile error:

IGYPS2055-S "EQUALS" was not defined as a class-name. The statement was discarded.


And to confirm @Bruce Martin's comment you can drop the IS, which is not referenced in the table.

works

=

IF name1 = name2
    DISPLAY "Names are the same"
ELSE
    DISPLAY "Names are not the same"
END-IF.