AZK RPG - Arrays - C11-1-323

  1. Array
    - is a grouping of data that contains multiple elements of the same type
  2. Dimension
    Dim - keyword to specify the number of elements in array / table
  3. Array as stand-allong field
    • Dcl-s Abbrev  Char(2)   Dim(50);
    • Dcl-s State   Char(14)  Dim(50);
    • Dcl-s Taxrate Char(5:5) Dim(50);
  4. Array Initialization
    • Taxrate(2) = 0.4;
    • Taxrate(3) = 0.65;
    • Taxrate(4) = 0.56;
    •  ...
    • Taxrate(50) = 0.4;
  5. Arrays as subfields of DS
    • Dcl-ds Taxes Qualified Inz;
    •    Dcl-s Abbrev  Char(2)   Dim(50);
    •    Dcl-s State   Char(14)  Dim(50);
    •    Dcl-s Taxrate Char(5:5) Dim(50);
    • End-ds;

    • Taxes.Abbrev(1)  = 'AK';
    • Taxes.State(1)   = 'Alaska';
    • Taxes.Taxrate(1) = 0;
  6. Array as DS
    • - must be run-time array
    • - must have Qualified keyword

    • Dcl-ds Taxes Dim(50) Qualified Inz;
    •    Dcl-s Abbrev  Char(2);
    •    Dcl-s State   Char(14);
    •    Dcl-s Taxrate Char(5:5);
    • End-ds;

    • Taxes(1).Abbrev  = 'AK';
    • Taxes(1).State   = 'Alaska';
    • Taxes(1).Taxrate = 0;

    •           or
    • Taxes(1) = 'AKAlaska        00000';
  7. Array as Ext Desc DS
    • Dcl-ds CustDS Ext Extname('CUSTOMERST')
    •                       Dim(50) Qualified
    • End-ds;
  8. Array values can come from Data Base File / Table (Example)
    • Dcl-s HAbbrev  Char(2);
    • Dcl-s HState   Char(14);
    • Dcl-s HTaxrate Char(5:5);
    • Dcl-s X        Uns(5);
    • Dcl-c Endoffile '02000';

    •      // Array as DS
    • Dcl-ds Taxes Dim(50) Qualified Inz;
    •    Dcl-s Abbrev  Char(2);
    •    Dcl-s State   Char(14);
    •    Dcl-s Taxrate Char(5:5);
    • End-ds;

    •      // Cursor Declaration
    • Exec SQL Declare StateCursor For
    •    Select Abbrev, State, Tazrate
    •    From Statefile
    •    Order By Abbrev
    •    For Read Only;

    •      // Open Cursor
    • Exec SQL Open StateCursor;

    •      // Loading Array values from table Statefile
    • For X = 1 To %Elem(Taxes);

    •    Exec SQL Fetch StateCursor
    •       Into :HAbbrev, :HState, :HTaxrate;

    •    Select;
    •       When Sqlstate = Endoffile;
    •          Leave;
    •       Other;
    •          Taxesf(X).Abbrev = HAbbrev;
    •          Taxesf(X).State = HState;
    •          Taxesf(X).Taxrate = HTaxrate;
    •    Endsl;
    • Endfor;

    •      // Close Cursor
    • Exec SQL Close StateCursor;
  9. Updating file Statefile with changed data from array values
    • For X = 1 To %Elem(Taxes);
    •    HAbbrev = Taxes(X).Abbrev;
    •    HState = Taxes(X).State;
    •    HTaxrate = Taxes(X).Taxrate;

    •    Exec SQL Update Statefile
    •       Set State = HState
    •             Taxrate = HTaxrate
    •       Where Abbrev = HAbbrev;
    • Endfor;
  10. Loading run-time array from DS overlaying subfields
    • Dcl-ds *N;
    •    *N   Char(9) Inz('Sunday');
    •    *N   Char(9) Inz('Monday');
    •    *N   Char(9) Inz('Tuesday');
    •    *N   Char(9) Inz('Wednesday');
    •    *N   Char(9) Inz('Thursday');
    •    *N   Char(9) Inz('Friday');
    •    *N   Char(9) Inz('Saturday');
    •    Days Char(9) Dim(7) Pos(1)
    • End-ds;

    • - Days
    •    - is an array-subfield of DS w Dim 7 starting in Pos 1 overlaying each previous subfield. So, array Days is loaded w DS subfield's values

    Days = 'Sunday   Monday   Tuesday  WednesdayThursday ... '
  11. Runtime Array and Ext Desc File
    • - We have PF
    • - Each record contains a salesperson's ID Number and total sales for each month during the last year - 12 sales figures in all.

    • A           R Salesrec
    • A             Slnbr        5A      Text('Person ID')
    • A             Sales      132A      Text('12 Months Sales')

    • - Sales field contains 12 figures, 11 w 2 dec each
    • - Sales length = 12 * 11 = 132

    • - In the program we have DS
    •      Dcl-ds *N;
    •         Sales      Char(132);
    •         SalesArray Zoned(11:2) Dim(12) Pos(1);
    •      End-ds;
    •                      OR

    - we can have PF w each month's sales figure saparately

    • A           R Salesrec
    • A             Slnbr        5A      Text('Person ID')
    • A             Sales01     11S 2    Text('January Sales')
    • A             Sales02     11S 2    Text('Februaty Sales')
    • A             Sales03     11S 2    Text('Marth Sales')
    •           .....................
    • A             Sales12     11S 2    Text('December Sales')

    • - In the program we have DS
    •       Dcl-ds *N;
    •          Sales01    Zoned(11:2);
    •          Sales02    Zoned(11:2);
    •          Sales03    Zoned(11:2);
    •                  ........
    •          Sales12    Zoned(11:2);
    •          SalesArray Zoned(11:2) Dim(12) Pos(1);
    •      End-ds;
  12. Compile-Time Array
    - obtains data from program source code
  13. Declaration of Compile-time Array in a program
    Dcl-s Days Char(9) Dim(50) Ctdata Perrcd(7);

    • - Dim(50)
    •    - array can contain 50 records
    • - Ctdata Perrcd(7)
    •    - array must contain 7 entries per record

    • - **Crtdata record
    •    - delemeter (saparator line) to identify the Compaletime Array
    •    - must be entered at the end of the program 
    •    - ** in posotion 1 and 2
  14. Compile-time Array - Days of the week (Example - 3 versions)
    • Version - 1
    • Dcl-s Days Char(9) Dim(50) Ctdata Perrcd(7);

    • **Crtdata Days
    • Sunday   Monday   Tuesday  WednesdayThursday Friday   Saturday

    • Version - 1
    • Dcl-s Days Char(9) Dim(7) Ctdata Perrcd(1);

    • **Crtdata Days
    • Sunday   
    • Monday   
    • Tuesday  
    • Wednesday
    • Thursday
    • Friday   
    • Saturday

    • Version - 3
    • Dcl-s Days Char(9) Dim(3) Ctdata Perrcd(3);

    • **Crtdata Days
    • Sunday   Monday   Tuesday
    • WednesdayThursday Friday
    • Saturday
  15. Related Arrays
    • - with Related Arrays we can load the data for 2 arrays in alternative format using
    • - Alt - Alternating Format keyword

    • Dcl-s Abbrev Char(2) Dim(50) Ctdata;
    • Dcl-s State  Char(14) Dim(50) Alt(Abbrev);

    • **Crtdata Abbrev
    • AKAlaska
    • ALAlabama
    • ARArkansas
    •    . . .
    • WYWyoming
  16. %Elem or %Size in definition of any Array
    - State and Taxrate arrays inherit dimension of Abbrec array

    • Dcl-s Abbrev   Char(2)    Dim(50) Ctdata;
    • Dcl-s State    Char(14)   Dim(%Elem(Abbrev));
    • Dcl-s Taxrate  Zoned(5:5) Dim(%Elem(Abbrev));
    •               
    • Dcl-s State    Char(14)   Dim(%Size(Abbrev));
    • Dcl-s Taxrate  Zoned(5:5) Dim(%Size(Abbrev));
  17. %Elem
    - to get total number of elements in an array, table, or multiple-occurrence DS

    • - %ELEM(table_name)
    • - %ELEM(array_name)
    • - %ELEM(multiple_occurrence_data_structure_name)

                Elements = %Elem(Taxes);
  18. %Lookup
    - search an array for specific element value

    •     %Lookup - exact match
    •     %Lookuplt - less than
    •     %Lookuple - exact or less than
    •     %Lookupgt - greater than
    •     %Lookupge - exact or greater than
    •         
    •             X = %Lookup(Statecode:Abbrev);
  19. %Lookup - search Array DS by identifying one of its subfields as a key
    • Dcl-s X Uns(5) Inz;
    • Dcl-s Statecode Char(2) Inz('AZ');

    • Taxes(1) = 'AKAlaska        00000';
    • Taxes(2) = 'ALAlabama       04000';
    • Taxes(3) = 'ARArkansas      06500';
    • Taxes(4) = 'AZArizona       05600';
    •    . . .
    • Taxes(50) = 'WYWyoming       00000';

    • X = %Lookup(Statecode:Taxes(*).Abbrev);  // X = 4
    •           - X is an index to refer to the element
    • Taxes(X).Abbrev  is AZ
    • Taxes(X).State   is Arizona
    • Taxes(X).Taxrate is .056
  20. %Xfoot
    - Sum of elements of an array

    Ytdsales = %Xfoot(Monthlysales);
  21. %Subarr
    - Set/Get Portion of an array

    %Subarr(array-name : start-element {:elements})

    • Array_A = abc - def - ghi - jkl
    • Array_B = mn - op - qr - st - uv

    Array_B = %Subarr(Array_A:2:3);

    • - Array_A = abc - def - ghi - jkl
    • - Array_B = mn  - op  - qr  - st - uv - before
    • - Array_B = de  - gh  - jk  - st - uv - after
  22. Sorta
    - Sort an Array

    Sorta Array_A;
  23. Sorta - sort array by key subfield
    - program can refer to any of the subfields in Taxes by naming a subfield as a key

    Sorta Taxes(*).Abbrev;      // - sort array by key Abbrev
  24. Multiple-row Fetch
    • - to retrieve next single row from result set:
    •      Exec SQL Fetch cursor-name Into :host-variables/structure;

    • - if host-structure is an array DS, Fetch can perform multiple row fetch
    •      Exec SQL Fetch cursor-name For number Rows Into :host-structure;
  25. Multiple Row Fetch code example
    • Dcl-ds Customers Ext Dim(100) Qualified End-ds;
    •    . . .
    • Exec SQL Declare Custcursor Cursor For
    •    Select * From Customers
    •    Where Czip = :Czip;
    •    . . .
    • Exec SQL Open Custcursor;
    •    . . .
    • Exec SQL Fetch Custcursor For 100 Rows Into :Customers;

    • - 1st row   = Customers(1).Cfname, Customers(1).Clname,...
    • - 2nd row   = Customers(2).Cfname, Customers(2).Clname,...
    • - 100th row = Customers(100).Cfname,
  26. Incomplete Multiple Row Fetch
    • Multiple Row Fetch may not always retrieve all the rows called for.
    • We can use SQLCA subfield Sqlerrd
    • .
    •      Sqlerrd  Int(10) Dim(6);  // Diagnostic Information - array

    • - Sqlerrd(3)
    •    - number of rows that were successfully processed
    • - Sqlerrd(5)
    •    - number of rows fetched
  27. Incomplete Multiple Row Fetch code example
    • Ctl-opt Dftactgrp(*No);
    • .
    • Dcl-c Lastrow 100;
    • Dcl-s Nbrrows zoned(5)   inz(100);
    • Dcl-s Xlname Char(20) Inz('HERNANDEZ');
    • .
    • Dcl-s X Uns(5);
    • .
    •      // Host variables declaration
    • Dcl-s CUSTNOTx Char(9);
    • Dcl-s CFNAMETx Char(15);
    • Dcl-s CLNAMETx Char(20);
    • .
    • Dcl-C Max_SQL_Entries  10000;   // Max entries in DS
    • .
    •      // 1) Declare CustDS Array as Ext DS based on CUSTOMERST table
    • Dcl-Ds CustDS extname('CUSTOMERST')
    •    Dim(50) Qualified;
    • End-ds;
    • .
    •      //... if it is not compiled we need extra step
    •      // 1a) Declare SqlColumn Ext Ds based on CUSTOMERST table
    • Dcl-Ds SqlColumn extname('CUSTOMERST') Qualified;
    • End-Ds;
    • .
    •      // 1b) ... and now declare CustDS Array Like Ext DS SqlColumn including 10000 elements
    • Dcl-Ds CustDS LikeDS(SqlColumn) Dim(Max_SQL_Entries);
    • .
    •      // 2) Cursor declaration
    • Exec SQL Declare Cust CURSOR for
    •    Select * From Customerst
    •    Where Clnamet = :Xlname;
    • .
    • Exec SQL Open Cust;
    • .
    •      // at this point
    •      //     Lastrow = 100
    •      //     Nbrrows = 100
    •      //     Sqlerrd(5) = 0
    • .
    • Dou Sqlerrd(5) = Lastrow;
    • .
    •       // 3) ***** Multiple-row Fetch
    • .
    •    Exec SQL Fetch Cust For :Nbrrows Rows Into :CustDS;
    • .
    •     // Sqlerrd(5) = 100 - number of rows fetched
    •     // Sqlerrd(3) = 3 - number of rows successfully processed
    • .
    •      // 4) Check if any rows are processed
    • .
    •    If Sqlerrd(3) = 0;   // if 0 rows processed
    •       Leave;               // leave the loop
    •    Endif;
    • .
    •      // 5) Process each row individually based on X index
    •    For X=1 to Sqlerrd(3);
    •       CUSTNOTx = CustDS(x).CUSTNOT;
    •       CFNAMETx = CustDS(x).CFNAMET;
    •       CLNAMETx = CustDS(x).CLNAMET;
    •    Endfor;
    • Enddo;
    • .
    • Exec SQL Close Cust;
  28. Insert Multiple Rows
    - to insert multiple rows with a single statement by using an Array DS

    Exec SQL Insert Into table-name number Row Values (:host-structure);
  29. Insert Multiple Rows code example
    •      // 1) Declare CustDS Array as Ext DS based on CUSTOMERST table
    • Dcl-Ds CustDS extname('CUSTOMERST')
    •    Dim(50) Qualified;
    • End-ds;
    • .
    •      // Loop to populate 50 elements of CustDS
    • .
    •      // 2) Insert 50 rows into Customers table
    • Exec SQL Insert Into Customers 50 Rows Values (:CustDS);
  30. 2 dimensional Array
    Array as DS + DS subfield Array = 2 dimensional Array

    • Dcl-ds Galacxy Dim(8) Quailified;
    •    Sector Char(1) Dim(8);
    • Endds;

    •     S1 S2 S3 S4 S5 S6 S7 S8
    • G1
    • G2
    • G3                        XX
    • G4
    • G5 ------------------- Galaxy(5).Sector
    • G6
    • G7
    • G8

    • - Galaxy(3).Sector(6) - XX
    •    - refers to the 6th element of sector in the 3dr element of Galaxy

    • - Galaxy(5).Sector
    •    - refers to entire Sector array in the 5th element of Galaxy

    • - Galaxy.Sector
    •    - NOT ALLOWED
  31. 3 dimensional Array
    - Adding more dimensions to Array DS, using Likeds keyword = 3 dimensional Array

    • Dcl-ds Xtemp Template;
    •    Part Char(1) Dim(8);
    • End-ds;

    • Dcl-ds Warehouse Dim(8) Qualified;
    •    Pallet Likeds(Xtemp) Dim(8);
    • End-ds;

    • - Part
    •    - array-subfield of DS Xtemp- Dim(8)
    • - Warehouse
    •    - array DS - Dim(8)
    • - Pallet
    •    - subfield-array Dim(8), which consists parent's array-subfield Part Dim(8)
    • - Template
    •    - indicates that the program need NOT allocate memory at run-time. Only at compile-time for farther Like or Likeds declaration

    Warehouse(2).Pallet(3).Part(4)

       - We can omit the index only for rightmost array

    Warehouse(2).Pallet(3).Part
  32. Array Keys
    • - we can search and sort Array DS by identifying one of its subfields as a key
    • - key subfield cannot be an array
    • - only one key allowed
    • - used w opcode Sorta and function %Lookup
Author
flashsmilenet
ID
340501
Card Set
AZK RPG - Arrays - C11-1-323
Description
AZK RPG - Arrays
Updated