IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

How to Compare two variables in Cobol


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts Replace each space in cobol string wi... COBOL Programming 3
No new posts COBOL -Linkage Section-Case Sensitive COBOL Programming 1
No new posts COBOL ZOS Web Enablement Toolkit HTTP... COBOL Programming 0
No new posts Compare only first records of the fil... SYNCSORT 7
No new posts Calling DFSORT from Cobol, using OUTF... DFSORT/ICETOOL 5
Search our Forums:

Back to Top