ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Field Names within a CL

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

  • Field Names within a CL

    I want to assign a field name to a variable so I can just call the variable to evaluate a field against another variable.

    I have a file with 2 fields, FLD1, FLD2 and FLD3. Depending on the condition, I want to evaluate the field against &TSTVAR. The logic would be as folows.

    ------
    DO WHILE LOOP

    RCVF
    MONMSG CPF0864 EXEC(GOTO EOF)

    SELECT

    WHEN condition1

    IF &FLD1 EQ &TSTVAR THEN(CALL PGM1)

    WHEN condition2

    IF &FLD2 EQ &TSTVAR THEN(CALL PGM1)

    WHEN condition2

    IF &FLD3 EQ &TSTVAR THEN(CALL PGM1)

    ENDSELECT

    ENDDO
    -----

    This above would mean I have to evaluate the condition every time I read a record from the file. I would like to assign which field I want to evaluate from the start before I go through reading the entire file much like the following.

    -----
    SELECT

    WHEN condition1

    Assign the filedname FLD1 to &TSTFLD

    WHEN condition2

    Assign the filedname FLD2 to &TSTFLD

    WHEN condition2

    Assign the filedname FLD32 to &TSTFLD

    ENDSELECT

    DO WHILE LOOP

    RCVF
    MONMSG CPF0864 EXEC(GOTO EOF)

    IF &TSTFLD EQ &TSTVAR THEN(CALL PGM1)

    ENDDO
    -----

    How can I achieve that using a field name inside a variable and evaluate the corresponding field?

  • #2
    A field name inside a variable won't work, but you could set up a variable based on a pointer to overlay the field that you want, like so:
    Code:
    DCL        VAR(&PTR) TYPE(*PTR) ADDRESS(*NULL)
    DCL        VAR(&TSTFLD) TYPE(*CHAR) STG(*BASED) LEN(?) BASPTR(&PTR)
    
    SELECT
    WHEN condition 1
    CHGVAR     VAR(&PTR) VALUE(%ADDRESS(&FLD1))
    WHEN condition 2
    CHGVAR     VAR(&PTR) VALUE(%ADDRESS(&FLD2))
    WHEN condition 3
    CHGVAR     VAR(&PTR) VALUE(%ADDRESS(&FLD3))
    ENDSELECT
    
    DO WHILE LOOP
    RCVF
    MONMSG CPF0864 EXEC(GOTO EOF)
    IF &TSTFLD EQ &TSTVAR THEN(CALL PGM1)
    ENDDO
    Note that for this to work properly &FLD1, &FLD2, and &FLD3 all have to have the same type and length which will also be the type and length of &TSTFLD.

    Comment

    Working...
    X