Portal | References | Downloads | Info | Programs | JCLs | Mainframe wiki | Quick Ref

Author Message
Direction

New User

Joined: 15 Sep 2005
Posts: 11

 Posted: Tue Feb 28, 2006 1:53 pm    Post subject: How to Compare two variables in Cobol HI all I have to compare two variables having different value. Exp: var1 contains value 'xxxxx ' var2 contains value 'xxxxx' and IF (VAR1 EQUALS TO VAR2 ) ----> IS NOT COMING TRUE. How I can compare these two values other THAN DOING SUBSTRING. Pls help!!!!!!!!

donevin

New User

Joined: 07 Jun 2005
Posts: 70
Location: South Africa

Posted: Tue Feb 28, 2006 4:02 pm    Post subject: Re: COMPARING 2 VARIABLES IN COBOL

Would this help?

 Code: 01 Var1                Pic x(6). 01 Var-red redefines Var1.    03 Val              Pic x(5).    03 Filler           Pic x. 01 Var2                Pic x(5).

Then say : If Val = Var2
donevin

New User

Joined: 07 Jun 2005
Posts: 70
Location: South Africa

Posted: Tue Feb 28, 2006 4:06 pm    Post subject:

Reference modification would also be an option like so :
 Code: IF Var1(1:5) = Var2
Direction

New User

Joined: 15 Sep 2005
Posts: 11

 Posted: Tue Feb 28, 2006 6:04 pm    Post subject: Re: COMPARING 2 VARIABLES IN COBOL Thanx but I am looking for other solution. I know this why but it doesn't suit to my needs.
donevin

New User

Joined: 07 Jun 2005
Posts: 70
Location: South Africa

 Posted: Tue Feb 28, 2006 6:46 pm    Post subject: More information would help. What kind of solution are you looking for. Give more detail please.
Direction

New User

Joined: 15 Sep 2005
Posts: 11

 Posted: Tue Feb 28, 2006 8:25 pm    Post subject: Re: COMPARING 2 VARIABLES IN COBOL Thanx for ur help. I am comparing the two variable of different size, which may contain same value. As variables had a different size, if I got same value for both variables my if condition doesnot satisfies. How can I compare these to variables containing same value and having different size. IF ( var1 EQUAL TO var2 ) var1 and var2 have same value but different size.
donevin

New User

Joined: 07 Jun 2005
Posts: 70
Location: South Africa

 Posted: Tue Feb 28, 2006 8:38 pm    Post subject: These variables does not contain the same value as you know. Five x's plus a space can never be equal to five x's. I know numeric fields of different lengths but with the same value content would be equal, but there's no way string fields of different lengths can be equal. Are you comparing strings or numerics?
KS

New User

Joined: 28 Feb 2006
Posts: 91
Location: Chennai

 Posted: Fri Mar 03, 2006 11:46 am    Post subject: Hi, If its string , you can use STRCMP function. If its numeric , Use IF. If its between a numeric and alpha numeric, then move the numeric to alpha numeric and then use string function. Hope it helps you.Get back if am wrong. Thanks, KS
martin9

Active User

Joined: 01 Mar 2006
Posts: 290
Location: Basel, Switzerland

 Posted: Fri Mar 03, 2006 5:30 pm    Post subject: look at the hex values of your variables and you know if they are comparable... martin9
DavidatK

Active Member

Joined: 22 Nov 2005
Posts: 700
Location: Troy, Michigan USA

Posted: Fri Mar 03, 2006 11:28 pm    Post subject: Re: COMPARING 2 VARIABLES IN COBOL

Direction,

I have no problem with the comparison you are trying to make. The following code, and result, is what I get on my machine. What platform are you running on? Is it possible that the string in either field is not padded with spaces?

 Code: WORKING-STORAGE SECTION.                                                                                                            01  WS-STR1                     PIC X(20) VALUE 'DAVIDATK    '.    01  WS-STR2                     PIC X(8)  VALUE 'DAVIDATK'.                                                                                                                                            PROCEDURE DIVISION.                                                                                                                      IF WS-STR1 = WS-STR2                                              THEN                                                                  DISPLAY 'WS-STR1 = WS-STR2'                                    ELSE                                                                  DISPLAY 'WS-STR1 NOT = WS-STR2'                                END-IF.                                                                                                                              GOBACK.

give the result

 Code: .SARPAGE 4                  .                          .WS-STR1 = WS-STR2

Dave
Prajesh_v_p

Active User

Joined: 24 May 2006
Posts: 133
Location: India

 Posted: Wed May 31, 2006 12:12 pm    Post subject: Re: COMPARING 2 VARIABLES IN COBOL Direction.. Are u sure u had a problem like that?..What my understandig is,it will truncate the excess field and then comapre.. Does anybody why it happens if so?.. I too had a problem like this in recently..and I have got the result just like DavidatK explained..
calspach

New User

Joined: 05 May 2006
Posts: 32
Location: USA

 Posted: Sat Jun 03, 2006 3:42 am    Post subject: Perhaps you have low-values in that space instead of a space. Try initializing your variables before moving into them.
mmwife

Super Moderator

Joined: 30 May 2003
Posts: 1592

 Posted: Sun Jun 04, 2006 6:50 am    Post subject: Can you please show us the PICs of the 2 variables inquestion?
GeneNorman

New User

Joined: 26 May 2006
Posts: 8

Posted: Fri Jun 09, 2006 2:46 am    Post subject: READ THE MANUAL

For alphanumeric operands of unequal size
 Quote: Operands of unequal size If the operands are of unequal size, the comparison is made as though the shorter operand were extended to the right with enough spaces to make the operands equal in size.

In other words if the longer operance has ANY value but space in the positions that don't exist in the shorter operand the comparison is UNEQUAL
 All times are GMT + 6 Hours
 Page 1 of 1

Search our Forum:

 Topic Author Forum Replies Posted Similar Topics Missing Negative sign in COBOL jzhardy COBOL Programming 6 Sat Sep 26, 2020 11:32 am COBOL reading from SYSIN jzhardy COBOL Programming 1 Mon Sep 21, 2020 2:37 pm how to read accounting information in... jzhardy COBOL Programming 1 Mon Sep 21, 2020 9:57 am Compare two character variables Head ... Harold Barnes CLIST & REXX 3 Fri Aug 28, 2020 12:38 am NOT IN clause in COBOL pgm mramvivek COBOL Programming 8 Thu Aug 20, 2020 4:48 am

 © 2003-2020 IBM MAINFRAME Software Support Division
 Job Vacancies | Forum Rules | Bookmarks | Subscriptions | FAQ | Polls | Contact Us