RPGIV @ Work

A unique site for RPG and System i Lovers

Welcome!

Hi, this site will provide all what you need in System i and RPG developments.

My Name is Chamara Withanachchi, System i Expert and RPG Developer. And in the field for last 11 years.

I hope you will find lot of valuable information from this site

Moving to arrays in free format Print E-mail
User Rating: / 1
PoorBest 
Written by Chamara Withanachchi   
Moving to arrays in free format

I was moving valirble values to arrays using following methoud.

     D MyVariable      S             35A
     D MyArray         S              1A   Dim(35)
     D i               S              2  0

      /Free

        MyVariable = 'Move Array in Free Format';

        For i=1 to %Len(MyVariable);
          MyArray(i) = %SubSt(MyVariable : i : 1);
        EndFor;

        *INLR = *ON;

      /End-Free
	  

But later in Bob Cozzi's RPG tip I found a excellent methoud with memcpy C API

     H DFTACTGRP(*NO) ACTGRP(*NEW) BNDDIR('QC2LE')

     D MyVariable      S             40A
     D MyArray         S              1A   Dim(35)

     D MemCpy          PR                  ExtProc('__memcpy')
     D  Target                         *   Value
     D  Source                         *   Value
     D  Lenght                       10U 0 Value

      /Free

        MyVariable = 'Move Array in Free Format - This is Grate';

        MemCpy(%Addr(MyArray) :
               %Addr(MyVariable) :
               %Len(MyVariable));

        *INLR = *ON;

      /End-Free 

Also You can wrap the array in a DS and move the array to variable or variable to array

   D arrDs           DS                                    
   D  Arr                           1    Dim(30)           
   DVar              S             30                      
                                                           
    /Free                                                  
        var = 'Test Data to Array';                        
        arrDS = var;                                       
        arr(6) = ' ';                                      
        var = arrDS;                                       
                                                           
        *inLR = *On;                                       
    /End-Free                                              
<Previous   Next>