Populating a field in Cobol



Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

Populating a field in Cobol

Postby jyothp12 » Wed Mar 14, 2012 6:25 pm

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 ?
jyothp12
 
Posts: 14
Joined: Thu Dec 22, 2011 12:37 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Populating a field in Cobol

Postby Robert Sample » Wed Mar 14, 2012 7:47 pm

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.
Robert Sample
Global moderator
 
Posts: 3719
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times

Re: Populating a field in Cobol

Postby BillyBoyo » Thu Mar 15, 2012 5:51 am

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.
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 265 times


Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post