IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

How to get the value of a variable-the name is in a variable


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
SathyaS

New User


Joined: 20 Sep 2005
Posts: 11

PostPosted: Wed Feb 14, 2007 4:54 pm
Reply with quote

Hi,
We have a COBOL code with variables declared as:

WS-TEST1 PIC X(5)
WS-TEST2 PIC X(8)
WS-TEST3 PIC X(4)
.
.
.
WS-TEST100 PIC X(2)

The code has logic to populate all of these 100 variables (say it gets them from a file).

REQUIREMENT:
We want some mechanism by which if we pass the SYSIN as WS-TEST1 the code should display the value stored in WS-TEST1 variable alone.

SOLUTION?
One way to achieve this is:
ACCEPT WS-INPUT
IF WS-INPUT = ?WS-TEST1?
DISPLAY WS-TEST1
IF WS-INPUT = ?WS-TEST2?
DISPLAY WS-TEST2
and so on?

But this means we have to hardcode 100 conditions; and if the copybook for the file changes then we have to change the IF conditions.

Is there any other way to get the value displayed without hardcoding??
Back to top
View user's profile Send private message
prav_06
Warnings : 1

Active User


Joined: 13 Dec 2005
Posts: 154
Location: The Netherlands

PostPosted: Wed Feb 14, 2007 5:21 pm
Reply with quote

Hi Sathya,
U can use a table in order to do this, only if your variables are in a sequnce like u have mentioned
Code:
WS-TEST1 PIC X(5)
WS-TEST2 PIC X(8)
WS-TEST3 PIC X(4)
.
.
.
WS-TEST100 PIC X(2)

u can subscript the variable names as ws-test is 1 and ws-test2 is 2, so if ya give the subscript value inside your sysin dd * statement it would display the content of the same, I think this logic is better than hard coding all the 100 variable names.

Cheer's,

Thamilzan.
Back to top
View user's profile Send private message
guptae

Moderator


Joined: 14 Oct 2005
Posts: 1208
Location: Bangalore,India

PostPosted: Wed Feb 14, 2007 5:23 pm
Reply with quote

Hi Prav06,

But the picture clause of these variable are different
Back to top
View user's profile Send private message
prav_06
Warnings : 1

Active User


Joined: 13 Dec 2005
Posts: 154
Location: The Netherlands

PostPosted: Wed Feb 14, 2007 5:29 pm
Reply with quote

Oops!!!! icon_rolleyes.gif ... My mistake
Back to top
View user's profile Send private message
prav_06
Warnings : 1

Active User


Joined: 13 Dec 2005
Posts: 154
Location: The Netherlands

PostPosted: Wed Feb 14, 2007 5:36 pm
Reply with quote

guptae,
How about implementing the same logic that i had told, by defining the array of uniform size say x(8) which is the max. that has been posted in the variables
WS-TEST1 PIC X(5)
WS-TEST2 PIC X(8)
WS-TEST3 PIC X(4)
.
.
.
WS-TEST100 PIC X(2)

and moving the values of the variable to the elements of the array and then going thro.



Cheer's,

Thamilzan.
Back to top
View user's profile Send private message
guptae

Moderator


Joined: 14 Oct 2005
Posts: 1208
Location: Bangalore,India

PostPosted: Wed Feb 14, 2007 5:43 pm
Reply with quote

Hi Thamilzan,

This way it can be done ...by taking max size...

but again he need to load value of the variables individually bcoz they are coming from file
Back to top
View user's profile Send private message
nileshp

New User


Joined: 25 Feb 2006
Posts: 31

PostPosted: Mon Feb 26, 2007 6:00 pm
Reply with quote

Hi,
I this we can use "EVALUATE" to fulfill your need.
Lets consider following assumption

1)you r passing variable "WS-INPUT" from your JCL.
2) Accept it in the dataitem WS-INPUT
3) Use EVALUATE verb as follow

EVALUATE WS-INPUT
WHEN "WS-TEST1"
Display "WS-TEST1" WS-TEST1
.
.
.
.
WHEN OTHER
CALL error-proc
END-EVALUATE;


Regards
Nilesh Padwal
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Mon Feb 26, 2007 9:56 pm
Reply with quote

Hello,

How does data get into the variables?

If this same code "reads" something to load the variables, creating the variables the "max" length and redefining them as an array may work well. When the variables are loaded with data any issues of leading/trailing spaces/zeros could be dealt with as well as left/right justification.

Once the variables are built, they could be retrieved by using the input "field-name" as a "pointer". This will only work if the field-names always end with the relative field number.

Another approach would be to build variables in an array with 3 fields - one for field-name, one for actual data length, and one for the data (all of the data would be fixed at the longest valid length). You could then accept the "runtime field name" and look for it in the table. Once found, you could use the length of that field in a reference-modified move to move the data to "display" the data. To use this approach, i'd recommend making sure that all of the variables were loaded into the table left-justified and blank filled.
Back to top
View user's profile Send private message
jasorn
Warnings : 1

Active User


Joined: 12 Jul 2006
Posts: 191
Location: USA

PostPosted: Wed May 09, 2007 7:54 am
Reply with quote

I've been doing this kind of thing for years. Here's how I do it.
1. I wrote a program that reads a copybook and creates a different kind of copybook (meta data is the word I suppose). It has things like field name, start and stop positions, data type, etc.

2. Then I read that into an array in the program and use it instead of the copybook. Then you can use the field name in the logic. If you couple this with logic that lets you read different lrecls(i.e. record contains 0) you can have a nice utility that will work for all sorts of files as long as the field name in the metadata file matches.

You don't need the program in 1 if you don't mind creating the meta data file by hand.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts Extracting Variable decimal numbers f... DFSORT/ICETOOL 17
No new posts Variable Output file name DFSORT/ICETOOL 8
No new posts Moving Or setting POINTER to another ... COBOL Programming 2
No new posts parsing variable length/position data... DFSORT/ICETOOL 5
No new posts Masking variable size field - min 10 ... DFSORT/ICETOOL 4
Search our Forums:

Back to Top