Too Iterate or not…

Over the next couple of weeks, I will explore some of the reasons why I think managed environments are good for COBOL.

So.. lets the show on the road…

Setting up arrays/occurs items in COBOL and manipulating them can be painful. Lets look at some traditional code for playing around with a “months” table.


01 month-val.
05 FILLER PIC X(10) value "January".
05 FILLER PIC X(10) value "February".
05 FILLER PIC X(10) value "March".
05 FILLER PIC X(10) value "April".
05 FILLER PIC X(10) value "May".
05 FILLER PIC X(10) value "June".
05 FILLER PIC X(10) value "July".
05 FILLER PIC X(10) value "August".
05 FILLER PIC X(10) value "September".
05 FILLER PIC X(10) value "October".
05 FILLER PIC X(10) value "November".
05 FILLER PIC X(10) value "December".

01 month-tab redefines month-val.
05 months occurs 12 times.
10 month-NAME PIC X(10).

01 month PIC 99.

01 secondQuarterMonths pic x(70).

display "The months are:"
perform varying month from 1 by 1 until month equals 12
display " " months(month)
end-perform

display " "
display "The second quarter months are: "

string months(4) delimited by space
"/" delimited by size
months(5) delimited by space
"/" delimited by size
months(6) delimited by space
into secondQuarterMonths
on overflow
display "FATAL Error - secondQuarterMonths is too small"
stop run
end-string

display " " secondQuarterMonths

Now, imagine we can use one or two of the .Net’s base class libraries combined with some nifty natural extensions to COBOL to do some of the heavy lifting.

Defining the month item as a native .Net type ie: a string combined with using the base class libraries Split methods allows us to setup a array quickly.


01 commaDelimited string value
"January,February,March,April,May,June,July,August," &
"September,October,November,December".

01 months string occurs any.
01 month string.

01 secondQuarterMonths string.

set months to commaDelimited::"Split"(',')

display "The months are: "
perform varying month through months
display " " month
end-perform

display " "
display "The second quarter months are: "

set secondQuarterMonths to type "System.String"::"Join"('/', months, 3, 3)

display " " secondQuarterMonths

The .Net version, besides being smaller feels much easier to read, especially if you have any exposure to the .Net base class library from other languages such as C# or VB.Net.

Of course in a real world example, we don’t need to use the split method to setup an array, we could just use the “values” clause… for example:


01 months string occurs 12 values
"January" "February" "March" "April" "May" "June"
"July" "August" "September" "October" "November" "December".

01 month string.

perform varying month through months
display month
end-perform

If however you don’t want to hardcode the months, you can always use: CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName() in the System.Globalization namespace. 🙂

So… lets just do it…


01 Months string occurs any.
01 Month string.

set Months to type "System.Globalization.DateTimeFormatInfo"::
"CurrentInfo"::"MonthNames"

perform varying Month through Months
display Month
end-perform

Now, I hope you feel like I do that COBOL and .Net are indeed perfect partners to each other.


The above COBOL .Net code will execute on Micro Focus Net Express and I suspect it will also work on Fujitsu’s netCobol too.

This entry was posted in CLR, COBOL, Tips and tagged , , , . Bookmark the permalink.