2

I have a RPGLE subprocedure that returns an array of dim(100) as output. When the array is not filled completely, I am seeing the blanks in unused indexes. I need to get rid off those trailing blanks and return only the array indexes with values.

Dcl-Pr SubProc char(10) dim(100);

End-Pr

Is it possible in RPG IV?

jmarkmurphy
  • 11,030
  • 31
  • 59
Sadam Husain
  • 41
  • 1
  • 8

4 Answers4

1

Why does this matter? You defined the array with 100 elements, and that is what is going to be passed. You cannot define a variable length array to be returned. You can either process the array until you find a blank row, or you can return the array and count of rows in parameters like this:

dcl-pi *n;
  array     char(10) dim(100);
  length    int(5);
end-pi;

Or, come to think of it, you can return a data structure that contains an array and length like this:

dcl-ds rtnds_t  Qualified Template;
  length        int(5);
  array         char(10) dim(100);
end-ds;

dcl-pi *n likeds(rtnds_t);
end-pi;

Then process the returned data structure in a for loop.

jmarkmurphy
  • 11,030
  • 31
  • 59
  • Thank you for the Answer. I knew i could process until the blank or get the length, but I needed to get rid off the unused indexes. Is it impossible to do get rid off the unused array indexes in RPG ? – Sadam Husain Nov 09 '17 at 12:40
  • 2
    You can only pass a fixed length array. RPG does not have variable length arrays. – jmarkmurphy Nov 09 '17 at 15:38
1

As already mentioned it seems that a list is what is really wanted.

There are some list implementations in RPG, f. e.

Depending on what you want to do with the data you should choose either one or the other, see http://blog.rpgnextgen.com/blog/2017/02/19/lists-arraylist-vs-linked-list .

Documentation: http://iledocs.rpgnextgen.com and http://iledocs.sourceforge.net/docs/index.php?program=/QSYS.LIB/FIST1.LIB/QRPGLESRC.FILE/LLIST.MBR (for as along as the new ILEDocs is not generated for Linked List)

Examples: http://rpgnextgen.com/index.php?content=examples

Mihael
  • 364
  • 2
  • 4
0

You can remove trailing blanks in an array by returning a varchar array. However you'd still be returning the entire array.

DCL-PR dan1 varchar(10) dim(100);
end-pr;                           

...

mArray(1) = 'Hello';
mArray(2) = ' World';
   for k = 3 to %element(myArray);
      %len(mArray(k)) = 0;
   endfor;
return mArray;
danny117
  • 5,581
  • 1
  • 26
  • 35
  • 1
    With SQL a table with only used rows could be returned from a procedure. You could go that way. With Java my 2nd favorite part of java is the list functions. You could return a list of some type inculding only the used rows. – danny117 Nov 08 '17 at 18:27
  • np @SadamHusain – danny117 Nov 09 '17 at 18:00
0

IBMi Rest Web Services - Part3

dcl-ds wrapperDS qualified;
   ArrayName char(1) dim(999);
   ArrayName_LENGTH int(10);
end-ds;

for i = 1 to ArrayName_LENGTH;
   //Process each array entry in the loop
   println(ArrayName(i));
endfor;
Christoff Erasmus
  • 925
  • 1
  • 10
  • 26