Search code examples
arraysibm-midrange

How to shift array elements in RPG?


I am aware of the fact that A loop can be used to move each element to its new destination, but is there any simpler and more elegant solution for the problem? Clearly MOVEA doesn't help here too much, as it does not work on the same array.


Solution

  • Shift down using the SUBARR function.

     /FREE
        %subarr(array:1) = %subarr(array:2); // Shift elements
        array(%elem(array)) = *blanks;       // Reset the remaining element
     /END-FREE
    

    C                   EVAL      %SUBARR(array:1) = %SUBARR(array:2)
    C                   EVAL      array(%ELEM(array)) = *BLANKS
    

    Shifting up requires the use of a temporary array or storage due to overlap.