Hi again,
On 2/19/2010 2:06 PM, Scott Klement wrote:
I'm going to go over your code and fix it up, and post my results. Give
me a few minutes to do that...
Okay, here's the code posted by John Kelly with only some minor
revisions... this should solve the problem, and make it run slightly
faster as well:
h noMain bndDir('QC2LE') option(*srcstmt: *noDebugIO)
/include protoUtil
d getToken pr * extProc('strtok')
d pString * value options(*string)
d pDelimiters * value options(*string)
d delimiters s 2a inz('; ')
p get_eMail_Addresses...
p B export
d PI
d forSplit 1000a const
d numEmails 5i 0
d emails 75a dim(13)
d pToken s *
d split s 1000a varying
d emailIs s like(emails)
/free
split = %trim(forSplit);
pToken = getToken(split: delimiters);
numEMails = 0;
doW (pToken <> *null);
emailIs = %str(pToken);
if (emailIs <> *blanks);
numEmails += 1;
if (numEmails <= %elem(emails));
emails(numEmails) = emailIs;
endIf;
endIf;
pToken = getToken(*null: delimiters);
endDo;
return;
/end-Free
p E
p set_eMail_Addresses...
p B export
d PI
d forSplit 1000a
d emails 75a dim(13) const
d i s 5i 0
d split s 1000a varying inz('')
/free
for i = 1 to %elem(emails);
if (emails(i) <> *blanks);
if (%len(split) > 0);
split = split + ';';
endIf;
split = split + %trim(emails(i));
endIf;
endFor;
forSplit = split;
return;
/end-Free
P E
Here are the important points of what I changed:
1) I changed 'split' to a VARYING variable, and trimmed the blanks from
the end. It just seemed pointless to ask strtok() to find all of
those blanks at the end.
2) (the crucial one) I changed getToken(%addr(split): %addr(delimiters))
to be simply getToken(split: delimiters) and also changed
getToken(*null: %addr(delimiters)) to getToken(*null: delimiters)
This is the important change. Passing the strings directly lets RPG
null-terminate the properly, and therefore they don't go off into
random memory locations that follow the string, reading garbage
from memory.
3) I added CONST to the 2nd parameter of set_eMail_Addresses()...
another minor change, but it bugged me, so I changed it. You'll
need to make that change in your protoUtil copy book as well.
#2 was the big one.. that's what was causing the extra array elements
with garbage in them.
As an Amazon Associate we earn from qualifying purchases.