Tom : <Otherwise, you'll need to write a procedure for each data
type.>
This is exactly what I have right now - 1 procedure for each data type (or Field in the file) which means for 25 fields I need 25 procedures all having the same lines of code. What I wanted to do was make 1 procedure that could change which field it used by means of a call to that procedure. From what you tell me I can't do that. It seemed simple to me to be able to call the same procedure to do the same things only on different fields depnding on the calling parameters.
Present Code:
D Field1 1A
D Field2 10A
D Field3 25A
Procedure1
Get data for field1
Do 10 lines of code using Field1
End
Procedure2
Get data for field2
Do 10 lines of code using Field2
End
Procedure3
Get data for field3
Do 10 lines of code using field3
End
What I want:
Procedure1
Call Procedure4 using field1
Procedure2
Call Procedure4 using field2
Procedure3
Call Procedure4 using field3
Procedure4
Get data for passed parameter (field name)
Do 10 lines of code using passed field name
What this does is replace 30 line of code with 10 lines plus the extra bit for the parameter passing. When you have 25 procedures doing the same thing this means not coding 250 lines and you can euse the same code for different files thereby saving a whole lot more code. This is optimizing the ILE language by being able to reuse code. I just wasn't able to pass the field I wanted to use without having to define 25 - 125 variables and then code to find out which variable the procedure needed.
Both Scott and Adam seem to have an idea of what I'm trying to do and I'll give their suggestions a try and let you know how I make out. Thanks to all who replied and I do appreciate all the suggestions. The SQL is OK but I was trying to stay away from embedded SQL as we're not able to use free form SQL statements at this time (we are working on it).
Thanks again for all the help from list It has given me food for thought.
Don.
******************************************
Don Wereschuk
ISD - Programmer/Analyst
Simcoe Parts Service Inc.
Phone: 705-435-7814 Ex: 302
Fax: 705-435-6746
mailto:dwereschuk@xxxxxxxxxxxxxxx
******************************************
"Save the Cheerleader - Save the world" - Hiro Nakamura
-----Original Message-----
From: rpg400-l-bounces@xxxxxxxxxxxx [mailto:rpg400-l-bounces@xxxxxxxxxxxx] On Behalf Of Armbruster, Tom
Sent: Monday, January 21, 2008 5:37 PM
To: RPG programming on the AS400 / iSeries
Subject: RE: Passing Parameters
Don,
I don't think any of these will work since the 1st parameter is a value
that is not changed during the file reads. It basically tests the input
values up to 9999 times.
You can't assign a pointer value to a field that is not the same specs
as the address without an error. In other words, you can't define your
static variable as 50A if the field that is passed is not also 50A.
I understand what you're trying to accomplish, but a pointer is not the
solution in this case. If you want to write this procedure to work on
different field lengths, you should probably use dynamic SQL instead of
file reads. Otherwise, you'll need to write a procedure for each data
type.
P FillArrayAll B
D FillArrayAll PI
D FieldToCheck 10a options(*varsize) const
D FileName 10a options(*varsize) const
D CheckValue 50A const
D ArrayValue 50A const
D
D SQLString S 350A varying
D
D RecDS DS
D RecValue 50A
D**********************************************************
/FREE
SQLString = 'SELECT CAST(' + FieldToCheck + ' AS CHAR(50)' +
' FROM ' + FileName + ' OPTIMIZE FOR ALL ROWS';
Tom Armbruster
-----Original Message-----
From: rpg400-l-bounces@xxxxxxxxxxxx
[mailto:rpg400-l-bounces@xxxxxxxxxxxx] On Behalf Of Scott Klement
Sent: Monday, January 21, 2008 1:58 PM
To: RPG programming on the AS400 / iSeries
Subject: Re: Passing Parameters
Adam,
That's close to what I was about to suggest. But mine looks like this
instead:
P FillArrayAll B
D FillArrayAll PI
D FieldToCheck 50a options(*varsize)
D len 10i 0 value
D CheckValue 50A const
D ArrayValue 50A
D
D
D**********************************************************
/FREE
COUNT = 1;
read input filerec;
dow %subst(FieldToCheck:1:len)<%subst(CheckValue:1:len);
read input filerec;
enddo;
dow not %eof(input)
and %subst(FieldToCheck:1:len)=%subst(CheckValue:1:len)
and COUNT < 9999;
WrkSupp(COUNT) = ArrayValue;
COUNT += 1;
read input filerec;
enddo;
/END-FREE
P FillArrayAll E
You'd call it with something like:
FillArrayAll(filerec.PLCD: %len(filerec.PLCD): 'whatever': whatever);
You don't actually need to use a pointer for the first parameter, since
it's passed by REFERENCE... a pointer is passed under the covers
anyway. So when the subprocedure references 'FieldToCheck' it actually
references the exact same spot in memory as filerec.PLCD. Since that's
part of the filerec data structure, it'll reference the data in the data
structure, which is what is desired here.
Using %addr(), a pointer, and a based field accomplishes the exact same
thing, but requires a little more code and complexity to achieve the
same result.
I also used %subst() on CheckValue which Adam didn't -- and that might
be a result of me having an imprecise idea of what CheckValue is for.
But I did make CheckValue a CONST field since it's address is not
important, and that'll let you pass other CONST fields or literals if
you desire.
Note that this code (both Adam's and mine) will only work if all fields
are fixed-length alphanumeric fields. The results might be screwy,
otherwise.
Also, note that the way the code is written, the file has to be sorted
by the field you're checking! It reads records as long as the field
you're checking against is less than the check value, and that could
only be assumed to be correct if the file is sorted by that field (or if
it's reading a keyed access path by the field -- which doesn't seem
reasonable considering that the procedure is supposed to work on any
field.) This seems very strange to me -- and I wonder if that's not a
logic error in Don's original code.
Adam Glauser wrote:
Don Wereschuk wrote:
< How is the code inside the procedure supposed to know how long the
field
< is that you are passing?
Again I'm not passing actual data since I haven't read the file yet.
Oh, now I think I get what you're trying to do. Maybe you want
something more like this:
P FillArrayAll B
D FillArrayAll PI
D FieldToCheck *
D lenFieldToCheck...
D 5P 0 const
D CheckValue 50A
D ArrayValue 50A
D*
DCheckField S 65535A based(FieldToCheck)
D
D
/FREE
COUNT = 1;
read input filerec;
dow %subst(CheckField : 1 : lenFieldToCheck) < CheckValue;
read input filerec;
enddo;
dow not %eof(input) and
%subst(CheckField : 1 : lenFieldToCheck) = CheckValue and
COUNT < 9999;
WrkSupp(COUNT) = ArrayValue;
COUNT += 1;
read input filerec;
enddo;
/END-FREE
P FillArrayAll E
and you would call it like this:
fillArrayAll(%addr(filerec.PLCD) : %size(filerec.PLCD) :
'thisRecordNeedsAction' : 'I want this is my array');
As an aside, you seem to be using a global variable (WrkSupp) inside
your subprocedure. This is not a very good practice, and can lead to
some really frustrating bugs.
HTH,
Adam
--
This is the RPG programming on the AS400 / iSeries (RPG400-L) mailing
list
To post a message email: RPG400-L@xxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit:
http://lists.midrange.com/mailman/listinfo/rpg400-l
or email: RPG400-L-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at
http://archive.midrange.com/rpg400-l.
--
This is the RPG programming on the AS400 / iSeries (RPG400-L) mailing list
To post a message email: RPG400-L@xxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit:
http://lists.midrange.com/mailman/listinfo/rpg400-l
or email: RPG400-L-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at
http://archive.midrange.com/rpg400-l.
As an Amazon Associate we earn from qualifying purchases.