ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

REPLACING in Cobol's SELECT Statement

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • REPLACING in Cobol's SELECT Statement

    COPY...REPLACING in Cobol's SELECT Statement

    One of Cobol's great strengths is its ability to replace one
    string in a source member with another at compile time.
    This feature lets Cobol programmers create generic,
    pseudo- parametized "template" source members that can be
    used in a variety of different cases.


    For instance, you could declare a database file to a Cobol
    program in the ENVIRONMENT DIVISION using the following
    SELECT statement:

    Code:
    SELECT file-name
        ASSIGN TO DATABASE-os400name
        ORGANIZATION IS org-type
        ACCESS MODE  IS access-type.
    If, instead of hard-coding the SELECT statement,
    you preferred to create a generic source member to
    copy, you might try to code

    Code:
    COPY selectx REPLACING
        ==file-name==    BY ==cust-file==
        ==os400name==    BY ==custmast==
        ==org-type==     BY ==INDEXED==
        ==access-type==  BY ==SEQUENTIAL==.
    to obtain
    
    SELECT cust-file
        ASSIGN TO DATABASE-custmast
        ORGANIZATION IS INDEXED
        ACCESS MODE  IS SEQUENTIAL.
    Unfortunately, it doesn't work; the Cobol compiler chokes
    when it reaches DATABASE- os400name because COPY...
    REPLACING can replace only entire tokens, and os400name
    is not a token. DATABASE-os400name is the token.

    There are two solutions. The first one is to code the
    COPY...REPLACING statement to replace the entire token;
    in other words, to replace DATABASE-os400name with
    DATABASE- custmast. But that's clumsy.
    It's much better to write the SELECT statement as follows,
    with parentheses around the part of the token you want to replace:

    Code:
    SELECT file-name
        ASSIGN TO DATABASE-(os400name)
        ORGANIZATION IS org-type
        ACCESS MODE  IS access-type.
    Now you can code the COPY...REPLACING statement like this:

    Code:
    COPY selectx REPLACING
        ==file-name==    BY ==cust-file==
        ==(os400name)==  BY ==custmast==
        ==org-type==     BY ==INDEXED==
        ==access-type==  BY ==SEQUENTIAL==.
    This method will give you the result you want. The technique takes
    advantage of the fact that the open parenthesis is a valid terminator
    for a Cobol word. The parentheses cause the Cobol compiler to break
    DATABASE-(os400name) into four tokens: DATABASE-, (, os400name, and ).


    This technique produces compilable source code even though SEU's
    built-in syntax checker rejects the parentheses. To save yourself
    time and prevent frustration, turn off syntax checking by pressing
    the F13 key and typing an N (no) at the appropriate input field.

    Jon Paris
    All my answers were extracted from the "Big Dummy's Guide to the As400"
    and I take no responsibility for any of them.

    www.code400.com
Working...
X