Sunday, 17 September 2017

%CHECK Example

      * Program     :·CheckR
      * Description :·%Check() Example
      *
      * %Check()
      * Input  Parm1:·String of characters to be checked (Comparator)
      *        Parm2:·String in which the characters to be scaned/searched
      *
      * Output      :·First location of Parm2 string where char is not matched
      *               with Comparator's chars
      *
      * Note        :·0 will be returned if no exception found
      *==========================================================
      *

     D Digits          S             10    Inz('0123456789')
     D Vowals          S             10    Inz('AEIOU')

     C
      /Free
           Dsply %Check(Digits : '123A456') ;  // '4'

           Dsply %Check(Vowals : 'AEIOU') ;    // '0'

           Dsply %Check(Vowals : 'AEiOU') ;    // '3' CASE SENSITIVE
      /End-Free
     C                   SetOn                                        LR

%CHAR Example

      * Program     :·CharR
      * Description :·%Char() Example
      *
      * %Char()
      * Input  Parm1:·Number, date, time, timeStamp
      *        Parm2:·date or time format (When Parm1 is date or time)
      *
      * Output      :·Character String for Input parameter
      *
      *==========================================================
      *

     C
      /Free
           Dsply %Char(5) ;                    // '5'

           Dsply %Char(55.55) ;                // '55.55'

           Dsply %Char(d'2012-11-11' : *ISO) ; // '2012-11-11'

           Dsply %Char(d'2012-11-11' : *ISO0) ; // '20121111'
      /End-Free
     C                   SetOn                                        LR

%ALLOC Example

      * Program     :·AllocR
      * Description :·%Alloc() Example
      *
      * %Alloc()
      * Input       :·Memory size(positive integer) to be allocated
      * Output      :·Pointer to Allocated memory
      *
      * Note        :·dynamically allocated memory should be set free
      *               when not required, else it will cause memory leak
      *
      *==========================================================
      *
     D Var1            S             20    Based(Ptr)

     D Ptr             S               *

     C
      /Free
           Ptr = %Alloc(5) ;
           Dealloc Ptr ;

           Ptr = %Alloc(2*2 + 1) ;

           Var1 = 'Allocated' ;
           Dsply Var1 ;

           Dealloc Ptr ;
      /End-Free
     C                   SetOn                                        LR

Saturday, 16 September 2017

%ADDR Example

      * Program     :·AddrR
      * Description :·%Addr() Example
      *
      * %Addr()
      * Input       :·variable, array index, datastructure subfield
      * Output      :·Address of input variable
      *
      * Keyword Description
      *
      * Based       :·Variable is based on pointer
      *               Variable's value will change according to pointer
      *               location
      *==========================================================
      *
     D Arr1            S              6    Dim(2)
     D Arr2            S               *   Dim(2)                                Array of pointers

     D DS1             DS
     D  Fld1                          5
     D  Fld2                          5

     D Var1            S              5    Inz('aaaaa')
     D Var2            S              5    Based(Ptr2)
     D Var3            S              2  0 Inz(1)

     D Ptr             S               *                                         Pointer
     D Ptr2            S               *                                         Pointer

     C
      /Free
        // =================================================
        // For Variables
        // =================================================
           Ptr = %Addr(Var1) ;                 // Ptr --> Var1

           Ptr2 = %Addr(Var1) ;                // Ptr2 --> Var1

           If (Ptr = Ptr2) ;
              Dsply 'Ptr = Ptr2' ;             // True
           EndIf ;

           If (Var1 = Var2) ;                  // same location
              Dsply 'Var1 = Var2' ;            // True
           EndIf ;
        // =================================================
        // For Arrays
        // =================================================

           Ptr = %Addr(Arr1(Var3 + 1)) ;       // Ptr --> Arr1(2)

           // all members of Arr2 will have address of arr1(1)
           // Arr2(1) = Arr1(1)'s address
           // Arr2(2) = Arr1(1)'s address
           Arr2 = %Addr(Arr1) ;

           // members of Arr2 will have corresponding member address of Arr1
           // Arr2(1) = Arr1(1)'s address
           // Arr2(2) = Arr1(2)'s address
           Arr2 = %Addr(Arr1(*)) ;

        // =================================================
        // For Data Structure
        // =================================================

           Ptr2 = %Addr(Fld1) ;                 // Ptr2 --> Fld1

           Fld1 = 'bbbbb' ;
           Dsply Var2 ;                         // 'bbbbb'

      /End-Free
     C                   SetOn                                        LR