Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use variable name in a string to access certain variable

Status
Not open for further replies.

DMani

Programmer
Oct 22, 2007
1
IS
Hello everyone

I'm a total newbie in Cobol.

I have a case that I'm trying to figure out, and was wondering if someone out there had some info for me if it was a total bullshit trying that in Cobol
.

I have number of variables that all have the same name except for a numerator in the end of the var name. And I wanted to see if it was possible to generate string in a "for" loop and use that string to access a variable that has the same name as the string I generate.

example...

if I have those variables...

01 VAR-1 PIC X(6).
01 VAR-2 PIC X(6).
01 VAR-3 PIC X(6).

then I whould like to something like this...

PERFORM TEST AFTER VARYING counter FROM
1 BY 1 UNTIL counter = 3

* generate string from a string prefix and a numb postfix
string 'VAR-' counter

* and then use this string to set something in the
* VAR-1, VAR-2 and VAR-3

END-PERFORM.

Hope someone understands my problem and hope even more someone knows the answer:)

Best regards
D.Jonsson
 
Here's a simple one:

01 VAR PIC X(6)CCURS 3 TIMES.
01 I PIC 9(1) VALUE ZEROES.


PERFORM VARYING I FROM 1 BY 1
UNTIL (I > 3)
DO SOMETHING HERE
END-PERFORM.

 
OOPS. It should be

01 VAR PIC X(6) OCCURS 3 TIMES.
 
Use VAR(I) in the perform's body.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
If you need to keep the VAR-1, VAR-2 format, then have them defined first as 03 levels and then redefine as a table
eg.
Code:
01 VARS.
 03 VAR-1 PIC X(06).
 03 VAR-2 PIC X(06).
 03 VAR-3 PIC X(06).
01 VAR-RED REDEFINES VARS.
 03 VAR   PIC X(06) OCCURS 3.
01 I      PIC 9(1) VALUE ZEROES.

PERFORM
 VARYING I FROM 1 BY 1
 UNTIL I > 3
    MOVE VAR(I) TO ........ 
    etc
END-PERFORM

Hope this helps

Marc


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top