Tales of cryptic Cobol: strings

One of the more interesting things about Cobol is the way it deals with strings. Consider the following declaration is Cobol:

01 input-area.
    02 line1 pic x occurs 80 times.

If somewhere in the code, there is a motion to read a file:

read input-file into input-area 
    at end move zero to eof-switch

This will read a line of the file into input-area, which is technically a string. However you can’t access it element-wise, for that you need it’s sub-variable line1. And you can’t use line1 in functions that want to evaluate the whole string, for that you need input-area. Maybe the best of both worlds? This means that given the following declarations:

77 countTrailspc   pic 99.
77 linelength      pic 99.

We can build a paragraph that counts the length of a string, after stripping off trailing spaces. Something of the form:

    move 0 to countTrailspc.
    inspect function reverse(input-area) tallying countTrailspc for leading space.
    subtract countTrailspc from length of input-area giving linelength.

At the end the variable linelength will contain the length of the line.




Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.