Reputation: 770
just a short quick question.
How do you index an expression into a COBOL array?
For example, if my index k=1
, I would like to do the following to find an element of k=2
element(k+1)
Unfortunately this is not acceptable in COBOL and I would like to know if there is any alternative?
Upvotes: 0
Views: 2299
Reputation: 13076
I'm not sure why you think that won't work, as long as you put it in a Cobol statement.
ID DIVISION.
PROGRAM-ID. SUBMOD.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A-NICELY-NAMED-TABLE.
05 FILLER OCCURS 2 TIMES.
10 A-NICELY-NAMED-ENTRY PIC X.
01 ANOTHER-PIECE-OF-DATA PIC X VALUE SPACE.
01 A-NICELY-NAMED-SUBSCRIPT BINARY
PIC 9(4).
LINKAGE SECTION.
01 L-INPUT PIC X(4).
01 L-TO-HEX PIC BXBXBXBX.
PROCEDURE DIVISION USING L-INPUT L-TO-HEX.
MOVE "A" TO A-NICELY-NAMED-ENTRY ( 1 )
MOVE "B" TO A-NICELY-NAMED-ENTRY ( 2 )
MOVE 1 TO A-NICELY-NAMED-SUBSCRIPT
IF A-NICELY-NAMED-ENTRY ( A-NICELY-NAMED-SUBSCRIPT + 1 )
EQUAL TO "B"
MOVE A-NICELY-NAMED-ENTRY
( A-NICELY-NAMED-SUBSCRIPT + 1 )
TO ANOTHER-PIECE-OF-DATA
END-IF
DISPLAY ">" ANOTHER-PIECE-OF-DATA "<"
GOBACK
.
Output is:
>B<
With reference to your comment, it is not a "strictness" thing by any means. It is that "+ 1" is one thing, a "relative subscript", and "+1" is something else, it is a second subscript.
Depending on your compiler, you may be able to code:
MOVE ELEMENT(k++1) ...
You may have to put up with some moaning from the compiler, and I suppose in some it may not work. It would, however, but a horrible way to write Cobol.
I'd suggest not using names like ELEMENT. Too likely at some point in the future to appear as a "reserved word" for Cobol. Don't be shorthand. Use good names, use effective spacings. It'll help you understand your program a little later, and will help anyone else who has to look at it.
Upvotes: 3