Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

How to Compare two variables in Cobol

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming
View previous topic :: :: View next topic  
Author Message
Direction

New User


Joined: 15 Sep 2005
Posts: 11

PostPosted: Tue Feb 28, 2006 1:53 pm    Post subject: How to Compare two variables in Cobol
Reply with quote

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!!!!!!!!
Back to top
View user's profile Send private message

donevin

New User


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

PostPosted: Tue Feb 28, 2006 4:02 pm    Post subject: Re: COMPARING 2 VARIABLES IN COBOL
Reply with quote

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
Back to top
View user's profile Send private message
donevin

New User


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

PostPosted: Tue Feb 28, 2006 4:06 pm    Post subject:
Reply with quote

Reference modification would also be an option like so :
Code:
IF Var1(1:5) = Var2
Back to top
View user's profile Send private message
Direction

New User


Joined: 15 Sep 2005
Posts: 11

PostPosted: Tue Feb 28, 2006 6:04 pm    Post subject: Re: COMPARING 2 VARIABLES IN COBOL
Reply with quote

Thanx but I am looking for other solution. I know this why but it doesn't suit to my needs.
Back to top
View user's profile Send private message
donevin

New User


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

PostPosted: Tue Feb 28, 2006 6:46 pm    Post subject:
Reply with quote

More information would help. What kind of solution are you looking for. Give more detail please.
Back to top
View user's profile Send private message
Direction

New User


Joined: 15 Sep 2005
Posts: 11

PostPosted: Tue Feb 28, 2006 8:25 pm    Post subject: Re: COMPARING 2 VARIABLES IN COBOL
Reply with quote

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.
Back to top
View user's profile Send private message
donevin

New User


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

PostPosted: Tue Feb 28, 2006 8:38 pm    Post subject:
Reply with quote

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?
Back to top
View user's profile Send private message
KS

New User


Joined: 28 Feb 2006
Posts: 91
Location: Chennai

PostPosted: Fri Mar 03, 2006 11:46 am    Post subject:
Reply with quote

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
Back to top
View user's profile Send private message
martin9

Active User


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

PostPosted: Fri Mar 03, 2006 5:30 pm    Post subject:
Reply with quote

look at the hex values of your variables and you know if
they are comparable...

martin9
Back to top
View user's profile Send private message
DavidatK

Active Member


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

PostPosted: Fri Mar 03, 2006 11:28 pm    Post subject: Re: COMPARING 2 VARIABLES IN COBOL
Reply with quote

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
Back to top
View user's profile Send private message
Prajesh_v_p

Active User


Joined: 24 May 2006
Posts: 133
Location: India

PostPosted: Wed May 31, 2006 12:12 pm    Post subject: Re: COMPARING 2 VARIABLES IN COBOL
Reply with quote

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..
Back to top
View user's profile Send private message
calspach

New User


Joined: 05 May 2006
Posts: 32
Location: USA

PostPosted: Sat Jun 03, 2006 3:42 am    Post subject:
Reply with quote

Perhaps you have low-values in that space instead of a space. Try initializing your variables before moving into them.
Back to top
View user's profile Send private message
mmwife

Super Moderator


Joined: 30 May 2003
Posts: 1592

PostPosted: Sun Jun 04, 2006 6:50 am    Post subject:
Reply with quote

Can you please show us the PICs of the 2 variables inquestion?
Back to top
View user's profile Send private message
GeneNorman

New User


Joined: 26 May 2006
Posts: 8
Location: Madison, Wisconsin USA

PostPosted: Fri Jun 09, 2006 2:46 am    Post subject: READ THE MANUAL
Reply with quote

icon_rolleyes.gif 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
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts XML Parse for COBOL 5.2 Bhanu Praveen COBOL Programming 1 Sat Jul 08, 2017 8:58 pm
No new posts Dynamic output file creation in cobol... smileheal COBOL Programming 7 Thu Jun 15, 2017 10:53 pm
No new posts compare 2 rows within the same file ram_vizag SYNCSORT 7 Wed Jun 14, 2017 12:34 am
No new posts Search utility(3.14) using JCl and Cobol RKS3 COBOL Programming 12 Sat Jun 10, 2017 10:24 pm
No new posts COBOL DB2 program - zIIP eligible vasanthz COBOL Programming 7 Wed May 31, 2017 5:12 am


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us