Page 1 of 1

Populating a field in Cobol

PostPosted: Wed Mar 14, 2012 6:25 pm
by jyothp12
Hi,

I have to populate an amount field which is of PIC S9(11)V9(2) USAGE COMP-3 to an output file field PIC 9(13)V9(2) . Its working fine while moving.

The issue is : As per my requirement I need to enclose the amount in brackets if it is negative.For positive, populate as it is.

eg:

if amount is 33.33 , in output it will be 33.33
if amount is -33.33, in output it will be (33.33)

I have only a single amount field in output , in which I need to process the amount whether its positive /negative..

Is there anyway to implement this using Cobol ?

Re: Populating a field in Cobol

PostPosted: Wed Mar 14, 2012 7:47 pm
by Robert Sample
First, you need to get your terminology straight -- you can ADD or MOVE or COMPUTE (etc) a variable in COBOL. You do not "populate fields". In fact, the COBOL Language Reference manual has ZERO occurrences of the word "populate" -- which should be a clue to you that your terminology is wrong.

Second, you can do pretty much anything in COBOL -- if you're willing to spend the time and do the work.

Third, your post is not consistent. A PIC S9(13)V9(2) variable would not ever, under any circumstances, have the value 33.33 (positive OR negative) unless the code putting a value into the variable is in error. The V is an IMPLIED decimal point and as such a decimal point will not appear in the variable -- ever.

Fourth, your post is short of explanation -- since the output variable is 15 bytes, and "(33.33)" is only 7 bytes (ignoring the decimal point issue above) -- where did the other 8 bytes go? Is your value supposed to be centered? left-justified? right-justified?

Finally, making some of the usual assumptions, you could do something like:
05  VAR-X PIC X(18).
05  VAR-X-R REDEFINES VAR-X.
    10  VAR-LEFT PIC X(01).
    10  VAR-OUTPUT PIC 9(13).9(2).
    10  VAR-RIGHT PIC X(01).

MOVE IN-VAR TO VAR-OUTPUT.
INSPECT VAR-X-R TALLYING WS-SPACE-COUNT FOR LEADING SPACE
IF  IN-VAR < ZERO
    MOVE ')' TO VAR-RIGHT
    IF  WS-SPACE-COUNT = 1
        MOVE '(' TO VAR-LEFT
    ELSE
        MOVE '(' TO VAR-X-R (WS-SPACE-COUNT  : 1)
    END-IF
END-IF.
Warning: this code has NOT been tested.

Re: Populating a field in Cobol

PostPosted: Thu Mar 15, 2012 5:51 am
by BillyBoyo
There are some other ways to skin-the-cat. You've given little detail, so...

Somewhere, you have your numeric-edited field, lets say ---,---,--9,99. Immediately following the last digit on the right, and always in the same place, would be the closing bracket/parenthesis, always in the same place. Having done the above edit, or something similar, you have a number for printing which starts with a "-" if it is negative, but contains no "-" if it is positive.

If you have a look at INSPECT with REPLACING and TALLYING, you should be able to work out everything your need. Change the "-" to "(" if it exists, and if you make the change, put the ")" in its fixed position.

If you don't like the simplicity of the above, you could use UNSTRING to left-justify the number, and then STRING it along with appropriate "(" and ")" into a right-justified field.

You could do it the "old" way, with byte-by-byte moves using indexes/subscripts. You could use reference-modification. You could use Occurs Depending On. Others too.