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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How does one REPLACE unequal characters? 3

Status
Not open for further replies.

weberm

Programmer
Dec 23, 2002
240
US
I receive a transaction file in fixed field length format which contains dollar amounts which are processed by using the NUMVAL function. Recently, however, we began receiving dollar data enclosed in parentheses, which causes my program to crash. I was going to use an INSPECT ... REPLACE statement to replace the parentheses with zeros, but that would cause the data on the line to be shifted one bit. Is there a simple way to replace a character in a string with a different number of characters than the replaced portion?

EX:
000000000000000000,1278T,0000024.70,0000024.70,03/07/03
000000000000000000,1276T,(00070.06),(00070.06),03/07/03

becomes
000000000000000000,1278T,0000024.70,0000024.70,03/07/03
000000000000000000,1276T,0000070.06,0000070.06,03/07/03

by replacing '(' with '00' and ')' with NULL.
 
What if the '(' and ')' indicate a negative number? If you do not like my post feel free to point out your opinion or my errors.
 
> What if the '(' and ')' indicate a negative number?

Oops, I overlooked that!
The opening parenthesis needs to be replaced with '-0'.
 
I don't believe there is an elegant solution to this problem in COBOL. Depending on the situation, I've taken two general approaches to this sort of thing:

1. Brute force - copy the string from one location to another a byte at a time inserting or deleting characters as needed. For example:
Code:
*  Assume OUT-REC is at least IN-LENGTH characters long
*  IN-LENGTH is the length of the input string in IN-REC
MOVE SPACES                       TO OUT-REC
MOVE 1                            TO OUT-SUB
PERFORM VARYING IN-SUB FROM 1 BY 1
          UNTIL IN-SUB > IN-LENGTH
    EVALUATE IN-REC(IN-SUB:1) 
        WHEN "("
            MOVE "-"              TO OUT-REC(OUT-SUB:1)
            ADD 1                 TO OUT-SUB
        WHEN ")"
            CONTINUE
        WHEN OTHER
            MOVE IN-REC(IN-SUB:1) TO OUT-REC(OUT-SUB:1)
            ADD 1                 TO OUT-SUB
    END-EVALUATE
END-PERFORM
[\CODE]
2.  UNSTRING/STRING - This approach is fine when you have only one or perhaps two strings to replace but gets ugly after that.

Regards.

Glenn
 
PS - In this particular case, it may be quite acceptable to simply:
Code:
INSPECT IN-REC REPLACING "(" BY "-" 
                         ")" BY "0"
[\code]
That won't work if you have embedded parentheses elsewhere in the record or if the amounts that are surrounded by parentheses don't always include a decimal point.

Glenn
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top