Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi, I Want to compare a substrin 2

Status
Not open for further replies.

amarg

Programmer
Nov 19, 2002
106
IN
Hi,

I Want to compare a substring in COBOL how i can do that.

My strings is like that:

STR1= "1234321DUMMY3243212"

now i have to find out string "DEMMY" is in STR1 or not.

Please help me.

Amar
 
I think it might be best if you work with a table of letters

01 HZ-STR.
02 HZ-REC OCCURS 20.
03 HZ-LET PIC X.


MOVE "1234321DUMMY3243212" to HZ-STR.
MOVE ZERO TO POSITION_FOUND.
PERFORM VARYING X FROM 1 BY 1 UNTIL X > 15
IF HZ-LET(X) = "D"
AND HZ-LET(X+1) = "U"
AND HZ-LET(X+2) = "M"
AND HZ-LET(X+3) = "M"
AND HZ-LET(X+4) = "Y"
MOVE X TO POSITION_FOUND
MOVE 15 TO X
END-IF
END-PERFORM.

It's not optimal, and I doubt even usefull if you need to search for a variable string, but it's the only thing I can think of right now...






--------------------------------------
It's not the monsters under your bed, it is the men next door.
That make you fear, make you cry. Make you cry for the Child.
All the wars are fought amongst those lonely men. Unharmed, unscarred.
 
Hi,
Here is an easy way:

STR1 PIC X(18)
STR2 PIC X(18) VALUE SPACES
STR3 PIC X(18) VALUE SPACES

UNSTRING STR1 DELIMITED BY "DEMMY" INTO STR2, STR3.
IF STR2 = STR1 DISPLAY "DEMMY NOT FOUND".
 
That's a good sollution. Didn't cross my mind. Then again, I've only been doing COBOL profesionally for three months. --------------------------------------
It's not the monsters under your bed, it is the men next door.
That make you fear, make you cry. Make you cry for the Child.
All the wars are fought amongst those lonely men. Unharmed, unscarred.
 
I might suggest the following "improvement":
Code:
 IDENTIFICATION DIVISION.                
 PROGRAM-ID. TEST.
 DATA DIVISION.
 WORKING-STORAGE SECTION.
 01  WS-COUNT                 PIC S9(4) COMP.
     88  STRING-FOUND         VALUE IS +2.
 01  WS-SENDING-FIELD         PIC X(80).
 01  WS-RECEIVING-FIELD       PIC X.
 PROCEDURE DIVISION.
 0000-MAIN.
*   Set up a test string
     MOVE ALL "123"  TO WS-SENDING-FIELD
     MOVE "DENNY"    TO WS-SENDING-FIELD(50:5)
*   Check for substring
     INITIALIZE WS-COUNT
     UNSTRING WS-SENDING-FIELD
         DELIMITED BY "DENNY"
         INTO WS-RECEIVING-FIELD
              WS-RECEIVING-FIELD
         TALLYING WS-COUNT
     IF STRING-FOUND
         DISPLAY "String found"
     END-IF
     GOBACK
     .
[\code]
The advantages are:

It works with sending fields of any length without fooling around with the receiving fields.

The IF test afterwards is generally much faster than a full string compare.  

WARNING! WS-COUNT is not initialized by the UNSTRING.  You must take care of that yourself!

Also note that to generalize this procedure to look for a any substring and not the hard-coded literal "DENNY", you must use a reference modified data item with the proper length set (e.g.  WS-SUBSTRING(1:WS-LENGTH)) to account for trailing spaces (or use a data item reference that is exactly the right length for the DELIMITED BY).

Glenn
Brainbench MVP for COBOL II
 
For those 'allergic' to the UNSTRING verb, here is another solution. This alternative can be called to handle any search string of any length, located anywhere on the original buffer (edit-line).

All parameters (except sub) are set by the caller.
Code:
 01 edit-line         PIC X(80).
 01 edit-line-len     PIC 9(04) COMP VALUE 80.
 01 search-string     PIC X(10).
 01 search-string-len PIC 9(04) COMP 10.
 01 match-found-flag  PIC 9(04) COMP VALUE 0.
    88 match-found    VALUE 1.
 01 sub               PIC 9(04) COMP.


     PERFORM VARYING sub FROM 1 BY 1
     UNTIL (sub > (edit-line-len - search-string-len + 1)) OR match-found
       IF edit-line (sub:search-string-len)
         = search-string (1:search-string-len)
          MOVE 1 TO match-found-flag
       END-IF
     END-PERFORM
Dimandja
 
I can't test this. What's your view?
Code:
MOVE ZEROS TO WS-CNT
INSPECT WS-STRING TALLYING WS-CNT FOR LEADING 
        'DEMMY'

IF WS-CNT > 0
   SET DEMMY-FOUND    TO TRUE
ELSE
   SET NO-DEMMY-FOUND TO TRUE
END-IF
The manual is kind of vague on the permissible length of the FOR field.

Regards. Jack.
 
Jack,

It looks like the target ("DEMMY", here), must be 1 character or ALL (same character).

According to this particular manual, INSPECT has two purposes. It can count the number of times a particular character appears in an alphanumeric string, and it can change those characters to another.

INSPECT field-1 TALLYING field-2
FOR target.

'target' has a few different possibilities. It can be the word CHARACTERS in which case field-2 holds the number of characters in the alphanumeric string. Or a specific character can INSPECTed for using either ALL or LEADING followed by the character.

Dimandja
 
Hi D,

Yeah, that's what I originally thought, but the IBM manual has an example that shows "**". It's not exactly what I have but it uses more than 1 char. Here's what it looks like:

INSPECT ID_1 TALLYING ID_2 FOR ALL "**" REPLACING

ALL "**" BY ZEROS.

Wish I had access to a mainframe. It's easy enough to check.

The manual is:

IBM COBOL for MVS & VM IBM COBOL Set for AIX IBM VisualAge COBOL: Language Reference

Regards, Jack.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top