Page 1 of 1

Comparing data fields

PostPosted: Sat Oct 17, 2009 3:19 am
by chuckfoss
Hi - I'm having trouble comparing these 2 fields in my COBOL program:

Field1 = '000012345'
Field2 = '12345 '

Each is PIC X(9). Field2 has spaces after 12345.

Any suggestions?
Thanks.
Chuck

Re: Comparing data fields

PostPosted: Sat Oct 17, 2009 10:14 am
by swd
Chuck,

There are various things you can do to these fields such as using the INSPECT or UNSTRING COBOL verbs to manipulate your fields and strip out spaces or leading zeroes etc. This would then leave you with the two fields containing '12345', which would then compare OK. But is this what you want to do? The solution depends on what you are trying to achieve. If you have 2 PIC X(9) character fields that contain numeric data then should these fields really be PIC 9(9) fields?

Is the data example you give typical of the contents of fields, or could they contain many other values? Sorry my answer is a bit vague, but it would be good to know a bit more about what you trying to achieve. You can manipulate fields until your heart is content, but then does that invalidate the input data?

Re: Comparing data fields

PostPosted: Sat Oct 17, 2009 6:53 pm
by chuckfoss
SWD,

Thanks for your reply. Actually, field one is a PIC S9(9) field (000012345) and field two is PIC X(10) field (12345bbbbb) b=space. I have moved field one to a PIC X field in order to compare. The problem is that field two gets left justified before coming into my program. Both values represent an insurance code so they truly are equal.

Thanks for your help.
Chuck

Re: Comparing data fields

PostPosted: Sat Oct 17, 2009 6:54 pm
by chuckfoss
SWD,

Thanks for your reply. Actually, field one is a PIC S9(9) field (000012345) and field two is PIC X(10) field (12345bbbbb) b=space. I have moved field one to a PIC X field in order to compare. The problem is that field two gets left justified before coming into my program. Both values represent an insurance code so they truly are equal.

Thanks for your help.
Chuck

Re: Comparing data fields

PostPosted: Sat Oct 17, 2009 9:46 pm
by swd
OK, I'm still not really sure what you are trying to do, but it sounds like you are moving your field1 (000012345) numeric field to a character field2 '12345bbbbb' and then comparing field1 and field2? And as field2 is a PIC X field it left justifies the field and has trailing spaces, so the 2 fields don't match. This is as one would expect.

If this is you question (I may be totally barking up the wrong tree here!) then you could UNSTRING the field 2 into a numerc field and then compare.. something like this..

 
       IDENTIFICATION DIVISION.
       PROGRAM-ID. FORUM.
       ENVIRONMENT DIVISION.
       CONFIGURATION SECTION.
       DATA DIVISION.
       WORKING-STORAGE SECTION.

       01  WS-TEST-VAR-X                 PIC X(10).
       01  WS-TEST-VAR-9UST              PIC S9(9).
       01  WS-TEST-VAR-9                 PIC S9(9).

       LINKAGE SECTION.
       PROCEDURE DIVISION.
       MAIN-PROCESS SECTION.
      ******************************************************************
      *                        MAIN PROCESSING                         *
      ******************************************************************
           MOVE 12345                   TO WS-TEST-VAR-9
           MOVE 12345                   TO WS-TEST-VAR-X

           UNSTRING WS-TEST-VAR-X DELIMITED BY ALL SPACES
            INTO WS-TEST-VAR-9UST
           END-UNSTRING

           IF WS-TEST-VAR-9UST EQUAL TO WS-TEST-VAR-9
               DISPLAY 'OK'
           ELSE
               DISPLAY 'NOT OK'
           END-IF

           STOP RUN
           .

       MAIN-999-EXIT.
           EXIT.


The 2 fields will compare OK then. Does this help you in any way? I may have mis-understood your question, if so apologies and please post more detail.

Cheers
Steve.

Re: Comparing data fields

PostPosted: Sun Oct 18, 2009 12:34 am
by dick scherrer
Hello,

Is your goal to compare the numeric value? Will the pic x field always be valid?

You mention a sign (s9(9)) but the posted data shows no sign - if field1 had a sign the value would be 00001234E as the sign of a zoned-decimal number is the high-order nibble (1/2 byte) of the low-order byte.

If the pic x field will always be valid and you want to compare the numeric value, use the NUMVAL function and move the pic x field to another s9(9) field. Then compare the 2 pic s9(9) fields.

Possibly i misunderstand something - if so, please clarify.

Re: Comparing data fields

PostPosted: Mon Nov 02, 2009 4:10 am
by chuckfoss
Hi D.SCH., I solved the problem by using your suggestion of the NUMVAL Function.
Thank you VERY much!

Chuck

Re: Comparing data fields

PostPosted: Tue Nov 03, 2009 1:19 am
by dick scherrer
You're welcome - good to hear it is working - thanks for letting us know :)

Do keep in mind that if the pic x field ever contains other than a valid numeric, there will be an abend. . .

If the value was generated by a computer, it will most likely be ok.

d