MainframeSupports
tip week 11/2009:

One of the most irritating things about COBOL for me is its lacking ability to format numeric values in a way where they can by used in texts for instance error messsages. In order to use a numeric value in a text you must be able to left justify it and remove trailing blanks. I have tried to find a solution for this problem for a long time until I came up with the following solution:

* Somewhere in working storage section
01 d2cWS.
02 d2cI pic -(19)9.
02 d2cC redefines d2cI pic x(20).
02 d2cD pic x.
02 d2cO pic x(20).
02 d2cL pic s9(4) binary.

* Somewhere in procedure division
move something-numeric to d2cI
unstring d2cC delimited by all spaces
  into d2cD d2cO count d2cL
display 'Something numeric left justified ' d2cO(1:d2cL) ' with no trailing blanks'

I hope you can forgive me for the missing indentations to the correct columns in COBOL. I have chosen not to declare the variable something-numeric because it might be any kind of numeric variable in COBOL. The above piece of code is only able to handle integer values.

I have mentioned before that COBOL is not the most elegant programming language and unfortunately you cannot make a function out of the above piece of code in COBOL. Compared to many other operations in COBOL it is quite impressive that you are able to use only two statements for the task. On the other hand it requires five variable declarations.

Previous tip in english        Sidste danske tip        Tip list