1

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.

Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
Kofuku-san
  • 11
  • 1
  • 1
  • 2
  • `PIC` isn't a type, it's a syntactical construct that precedes the type (or rather, the format) of the field, in this case `x(20)` means "arbitrary text of 20 characters". – Dai Aug 31 '14 at 10:16
  • so basically I can't compare them? – Kofuku-san Aug 31 '14 at 10:18
  • @Dai No, `PIC X(20)` does not mean that. `X` is called "alphanumeric", but it can validly contain any of the 256 available bit-patterns. PIC X(20) is 20 bytes of data. What it contains depends on the context it is being used in. In the example, the `ACCEPT` is keyboard input, but PIC X fields can be used in many other ways. – Bill Woodger Sep 01 '14 at 00:02

2 Answers2

3

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.
Community
  • 1
  • 1
Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
  • 1
    It looks like EQUALS in GnuCOBOL (new name for OpenCOBOL) may be a Language Extension. Probably another older COBOL compiler allowed EQUALS, so GnuCOBOL does to allow that code to run. Yes, it won't work with any IBM COBOL compiler. – Bill Woodger May 13 '16 at 05:28
2

(Disclaimer: I'm not a COBOL user)

After some quick searching online, I found the OpenCOBOL reference manual ( http://opencobol.add1tocobol.com/OpenCOBOL%20Programmers%20Guide.pdf ) which describes the IS= relation operator (and its synonyms) working with string types (6.1.4.2.5.) to wit:

When comparing strings, the comparison is made based upon the program’s collating sequence (see section 4.1.2). When the two string arguments are of unequal length, the shorter is assumed to be padded (on the right) with a sufficient number of SPACES as to make the two strings of equal length. String comparisons take place on a corresponding character-by-character basis until an unequal pair of characters is found. At that point, the relative position of where each character in the pair falls in the collating sequence will determine which is greater (or less) than the other.

COBOL defines IS=, IS EQUAL TO, and EQUALS as equivalent operators, so you'll want something like this:

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

As the documentation states, your program's collation settings define how string comparison is performed, which means that potentially "Peter", "PETER", and "Péter" might be treated as equivalents.

The note about different lengths is interesting, the implicit right-padding means that these two strings are considered equal: ("Foo" and "Foo__") but not ("Foo" and "__Foo").

Dai
  • 141,631
  • 28
  • 261
  • 374
  • 1
    There is also the plain =, >= etc operators as well – Bruce Martin Aug 31 '14 at 22:36
  • 2
    You need to terminate your IF. The modern way is with END-IF. Otherwise a full-stop/period will do it. COBOL does not have "strings", in the sense of a delimited string of textual data, it has fixed-length fields. As such, the relative position of each byte of a field is compared, with the padding as you have identified. If you want to find if `Foo` is in a field with or without trailing space, it is unproblematic. Collating sequence use is rare, but yes, I think you could get it to consider those three equal. It would apply to all data in the program, so use with care :-) – Bill Woodger Aug 31 '14 at 23:56
  • Oddly enough, the `EQUALS` syntax doesn't work for me, but all of the others listed in the manual do. – Jeff Puckett May 13 '16 at 03:10
  • 1
    @JeffPuckettII that's because it is non-Standard. – Bill Woodger May 13 '16 at 05:32