Replacing a larger string with smaller string

This is a Mainframe COBOL forum - you can post your queries on Mainframe COBOL, VS COBOL II, COBOL/370 , Enterprise COBOL

Moderators: dbzTHEdinosauer, Moderator Group

Post Reply
ashok6254
Member
Posts: 3
Joined: Mon Oct 24, 2011 11:16 am
Location: Chennai

Replacing a larger string with smaller string

Post by ashok6254 » Fri Mar 09, 2012 4:43 pm

Hi,

I am trying to replace a larger byte string with a smaller byte string. I tried using cobol INSPECT - REPLACING.. As both operands are not of equal size, it is throwing an error.

Suppose in a 2000 byte string, if there is any "SHOULD BE", should be replaced with "S/B". i.e., a 9 bytes string should be replaced with a 3 bytes string. cant tell where this string may come and how many times it may come.

Eg:(for reference)

"THIS SHOULD BE DONE TODAY" ===> "THIS S/B DONE TODAY"


Could someone suggest me how I can do this.


Thanks,
Raju

User avatar
dbzTHEdinosauer
Moderator
Posts: 981
Joined: Mon Oct 02, 2006 8:31 pm

Post by dbzTHEdinosauer » Fri Mar 09, 2012 5:33 pm

how many times can/could the string 'SHOULD BE' occur within the 2000 char string?

i would UNSTRING text DELIMITED BY 'SHOULD BE'
and then STRING the components inserting 'S/B' between each component.

of course you need to define your receiving fields of the UNSTRING to insure enough length.

i would INSPECT TALLYING FOR 'SHOULD BE' before I UNSTRING,
not bothering to UNSTRING if no occurances,
and the TALLY special register will tell you how many components you will have (TALLY + 1)

to make it easy, i would:
1. INITIALIZE the receiving fields group area with '@".
thus the STRING of the components would by DELIMITED BY '@"

2. have several STRING statements, depending on the TALLY value.
i.e. if tally is 1, then use

Code: Select all

STRING receiving-field-1 
                 DELIMITED BY '@'
       'S/B' 
                 DELIMITED BY SIZE
       receiving-field-2 
                 DELIMITED BY '@'
  INTO new-2000-byte-area
END-STRING
if tally is 2, then use

Code: Select all

STRING receiving-field-1 
                 DELIMITED BY '@'
       'S/B' 
                 DELIMITED BY SIZE
       receiving-field-2 
                 DELIMITED BY '@'
       'S/B' 
                 DELIMITED BY SIZE
       receiving-field-3 
                 DELIMITED BY '@'
  INTO new-2000-byte-area
END-STRING
if tally is 3, then use

Code: Select all

STRING receiving-field-1 
                 DELIMITED BY '@'
       'S/B' 
                 DELIMITED BY SIZE
       receiving-field-2 
                 DELIMITED BY '@'
       'S/B' 
                 DELIMITED BY SIZE
       receiving-field-3 
                 DELIMITED BY '@'
       'S/B' 
                 DELIMITED BY SIZE
       receiving-field-4 
                 DELIMITED BY '@'
  INTO new-2000-byte-area
END-STRING
ETC....
Dick Brenholtz
JCL, SQL and code in programs have an irritating habit of doing what you say,
not what you meant.

ashok6254
Member
Posts: 3
Joined: Mon Oct 24, 2011 11:16 am
Location: Chennai

Post by ashok6254 » Fri Mar 09, 2012 5:49 pm

how many times can/could the string 'SHOULD BE' occur within the 2000 char string?
not sure...

It may occur once or twice or thrice or in worst case more than 10 times..

And can't tell where this may come.....

ashok6254
Member
Posts: 3
Joined: Mon Oct 24, 2011 11:16 am
Location: Chennai

Post by ashok6254 » Tue Mar 13, 2012 3:18 pm

Got it....

Thank you dick for your suggestion..

I did this way..

Code: Select all

inspect msg-2000-chars tallying cnt-sb for all "should be"

perform cnt-sb times
    inspect msg-2000-chars replacing first "should be" by "s/b{*}{@}"

    unstring msg-2000-chars delimited by "{@}"
          into receiving-field1
               receiving-filed2
    end-unstring

    string receiving-field1 delimited by "{*}"
           receiving-field2 delimited by size
       into msg-2000-chars
    end-string
end-perform.

Post Reply

FREE TUTORIALS

Tutorials
Free tutorials from mainframegurukul
  • JCL Tutorial
    Covers all important JCL concepts.
  • Cobol Tutorial
    This tutorials covers all Cobol Topics from STRING to COMP-3.
  • DB2 Tutorial
    DB2 Tutorial focuses on DB2 COBOL Programming.
  • SORT Tutorial
    This Tutorial covers all important aspects of DFSORT with examples
  • CICS Tutorial
    This CICS tutorial covers CICS concepts and CICS Basics, CICS COBOL Programming.
Interview
Mainframe Interview questions



Other References
Mainframe Tools and others