Session 9 Tables and Arrays

download Session 9 Tables and Arrays

of 23

Transcript of Session 9 Tables and Arrays

  • 7/23/2019 Session 9 Tables and Arrays

    1/23

    RPG/400

    Session 9

  • 7/23/2019 Session 9 Tables and Arrays

    2/23

    INTRODUCTION

    Arrays and tables allow ro!ra" to

    or!ani#e !ro$s o% related data ele"ents

    %or easy retrie&al'

    Data str$(t$res allow to de%ine an area o%

    sa(e wit)in "e"ory to store indi&id$al

    ele"ents o% data'

    *le"ents are s$b%ields wit)in a lar!er

    str$(t$re'

  • 7/23/2019 Session 9 Tables and Arrays

    3/23

    TA+,*S

    Store fixed set of data elements for lookup

    Generally used when fixed number of possible elementsare in the list.

    Used for validation

    To extend usefulness of tables RPG allows to define

    second table related to the first. A related table is a list of elements associated with

    correspondin element with the primary table.

    Tables cab be loaded at !ompile time or at pre"runtime.

    !ompile time tables are loaded with data included in thesource code.

    Pre runtime tables load from the #atabase file durininitili$ation phase of the proram execution.

    Table names bein with TA%.

  • 7/23/2019 Session 9 Tables and Arrays

    4/23

    Arrays

    O%%ers "ore %le-ibility in%$n(tion t)an table

    Pro!ra" (an dyna"i(allyre%eren(e and "ani$lateindi&id$al ele"ents d$rin!

    e-e($tion One (o""on $se is to )old

    s$""ary totals d$rin!e-e($tion

    Arrays (an be sorted and

    n$"eri( &al$es (an be totaled$sin! sin!le o.(ode'

    Arrays are loaded at (o"ileti"ere.r$nti"e and r$nti"e'

  • 7/23/2019 Session 9 Tables and Arrays

    5/23

    Co"ile ti"e arrays are loaded wit) data

    in(l$ded in t)e so$r(e (ode'

    Pre r$n ti"e arrays load %ro" t)edatabase %ile d$rin! initiali#ation )ase o%

    ro!ra" e-e($tion'

    R$nti"e arrays are loaded wit)in t)ero!ra" lo!i( d$rin! e-e($tion'

    Array ele"ents (an be a((essed $sin! an

    inde-'

  • 7/23/2019 Session 9 Tables and Arrays

    6/23

    Co"ile Ti"e table

    De%inition

    D TA+NA*S S 10 DI23

    CTDATA

    P*RRCD25 *a() ele"ent de(lared as 10 ()ara(ter len!t) DI 6eyword se(i%ies "a-i"$" n$"ber ele"ents

    in table as 3

    CTDATA 6eyword indi(ates table is loaded $sin!(o"ile ti"e data e"bedded in t)e so$r(e' P*RRCD indi(ates )ow "any ele"ents in table are

    loaded %ro" ea() line o% so$r(e

  • 7/23/2019 Session 9 Tables and Arrays

    7/23

    #TA%S!&R' S ( ) #*+,-

    A/T,TA%0ames

    This #efines related table to store test scores.

    #eclares each element as ( diit numeric values with $erodecimals.

    The A/T keyword declares table as related or alternate tableof TA%0ames.

    1ou cannot specify P'RR!# of !T#ATA when A/T keywordis used.

    #ata used to load alternate table is loaded from the samesource record as the main table.

    The bottom of the source member contain the data used toload the table

  • 7/23/2019 Session 9 Tables and Arrays

    8/23

    77CTDATA TA+Na"esRA8 090

    IRAN 0:0

    8O* 0;3

    SIT< 0=9

    ANI, 0>3

    API, 0:1

    NI

  • 7/23/2019 Session 9 Tables and Arrays

    9/23

    C yNa"e ,OOUP TA+Na"es TA+S(ores

    C I? 7IN90 @ 7ON 90

    C *C*PT PrintItC *NDI?

    T)e St$dent na"e to be lo(ated is in ?a(tor 5 %or

    loo6$ oeration T)e loo6$ is er%or"ed on table TabNa"es

    I% ,oo6$ s$((eeds indi(ator 90 is seton and(orresondin! ele"ent TA+s(ore is ret$rned'

    T)e related table TA+SCOR* (ontains t)e(orresondin! t)ree di!it T*STSCOR* &al$e'

  • 7/23/2019 Session 9 Tables and Arrays

    10/23

    PR*RUNTI* TA+,*

    ?T*ST?I,* IT ? 11 DIS

    Table %ile is identi%ied by entry T in t)e Col osition 5: ?ile is oened %or in$t only'

    DTA+Na"es S 10 DI230

    P*RRCD25

    ?RO?I,*2Test?ile

    T)e na"e o% t)e tablesi#e and attrib$te o% ea()ele"ent "a-i"$" n$"ber o% ele"ents are de%ined in

    t)e D.Se( P*RRCD 6eyword de%ines )ow "any ele"ents o%table to load %ro" ea() line o% %ile re(ords' De%a$lt is 5'

    ?RO?I,* 6eyword indi(ates t)at t)e table is to be

    loaded $sin! re(ords in t)e %ile T*ST?I,*

  • 7/23/2019 Session 9 Tables and Arrays

    11/23

    D TA+SCOR* S = 0 DI230

    A,T2TA+Na"es

    *T?T2P

    Alternate table is de(lared

    +e(a$se test s(ore in t)e %ile is de%ine as

    a(6ed n$"eri( t)e e-ternal %or"at

    6eyword is $sed to "a t)e ne(essary

    translation

    P*RRCD and ?RO?I,* (annot bese(i%ied wit) t)e de%inition o% t)e A,T

    table'

  • 7/23/2019 Session 9 Tables and Arrays

    12/23

    Co"ile Ti"e Array

    D NA*S S 10 DI23

    CTDATA

    P*RRCD25

    DI indi(ates (aa(ity o% t)e array

    CTDATA indi(ates it is a (o"ile ti"e array and

    re(ords are loaded %ro" botto" o% t)e %ile' P*RRCD de%ines )ow "any re(ords to be

    loaded %ro" ea() line o% so$r(e'

  • 7/23/2019 Session 9 Tables and Arrays

    13/23

    77CTDATA Na"e

    8o)n

    S"it)

    ABay8oe

    77CTDATA TestS(ore

    0:30=3

    030

    095

    *a() CTDATA be!ins in (ol$"n 0

    Data is loaded seerately and not relati&ely as in t)e(ase o% tables )ere as it (an be related $sin! inde-'

  • 7/23/2019 Session 9 Tables and Arrays

    14/23

    '4A/ x 5 2

    +yname /&&6UP 0ame,x 7)

    *8 9*07) 5 9&0

    :8&&T T'STS!&R' T&TS!&R'S

    ':!'PT Printit

    '0#*8

    Purpose of index

    *ndex sets the startin point for lookup to bein

    *f lookup is successful index contains the element number where

    lookup value is found.

    :8&&T offers 3uick way to add each element toether until

    total is derived from the end.

    8ield name specified in the result field holds the sum of the

    array elements.

  • 7/23/2019 Session 9 Tables and Arrays

    15/23

    Pre.R$nti"e Array

    ? T*ST?I,* CT ? 11 DIS

    D NA* S 10 DI230

    P*RRCD25

    ?RO?I,*2Test?ileTO?I,*2Test?ile

    D T*STSCOR* S =0 DI230

    A,T2Na"e

    *T?T2P ?ile to be de(lared in (o"bined "ode as re(ords are

    ()an!ed d$rin! e-e($tion' T)e &al$es in t)e %ile are ()an!ed a%ter t)e ro!ra"

    e-its'

  • 7/23/2019 Session 9 Tables and Arrays

    16/23

    #: S ( )

    # +yname S ;) *0oe?

    # T&TScore S ( )

    ! '4A/ x 5 2

    ! +yname /&&6UP 0ame,x 7)

    *8 9*07) 5 9&0

    '4A/ T'STS!&R' 5 77

    :8&&T T'STS!&R' T&TS!&R'S

    ':!'PT Printit

    '0#*8

    '4A/ 9*0/R 5 9&0

    R'TUR0

    &@PR*0T Printit2

    0ame,x ;-

    TestScores < ()

    ( =T&TA/B?

    T&TS!&R'S < C;

  • 7/23/2019 Session 9 Tables and Arrays

    17/23

    R$n Ti"e Arrays

    T)e e-a"le $ses two arrays )oldin!na"es and asso(iated s(ores'

    T)e ro!ra" rints t)e ori!inal se$en(e

    T)e na"e array is sorted in as(endin!order and rinted

    T)e s(ore array is sorted in as(endin!

    order and rinted' T)e e-a"le ill$strates r$nti"e o&erlayin!

    o% arrays'

  • 7/23/2019 Session 9 Tables and Arrays

    18/23

    ?T*ST?I,* I? * DIS

    ?EPRINT O ? 5=1 PRINT*R

    D DS

    DSTUD*NTS 1= DI23D Na"e 10 O&er,ay2St$dentsF5

    DTestS(ore = 0 o&erlay2St$dentsF15

    D In#

    D- S = 0

    DArraySi#e S = 0

    IN2H*le"2St$dents

  • 7/23/2019 Session 9 Tables and Arrays

    19/23

    7.................................

    7 ,oad T)e Arrays

    7.................................

    C DOU 7In90 @ 7On

    C Read StRe( 90

    C I% 7In90 @ 7o%%

    C *&al - @ - 5C I% - J@ ArraySi#e

    C *&al Na"e2 @ STN

    C *&al TestS(ore2 @ STSCC *ndI%

    C *ndI%

    C *ndDo

    7.........................................

  • 7/23/2019 Session 9 Tables and Arrays

    20/23

    9"""""""""""""""""""""""""""""""""""""""""

    9Print Array !ontents in &riinal Se3uence

    9"""""""""""""""""""""""""""""""""""""""""

    ! 'xcept Dead2

    ! #o ArraySi$e :

    ! 'xcept Print*t

    ! 'nd#o

    9""""""""""""""""""""""""""""""""""""

    9 Sort Arrays %y 0ame And Reprint

    9""""""""""""""""""""""""""""""""""""

    ! Sorta 0ame

    ! 'xcept Dead;

    ! #o ArraySi$e x

    ! 'x!ept Print*t

    ! 'n##o

    9""""""""""""""""""""""""""""""""""""

  • 7/23/2019 Session 9 Tables and Arrays

    21/23

    7....................................

    7 Sort Arrays +y TestS(ore And Rerint

    7....................................

    C Sorta TestS(ore

    C *-(et

  • 7/23/2019 Session 9 Tables and Arrays

    22/23

    &9

    &@PR*0T ' Dead2 2 )2

    & () E"" 0ot Sorted ""E& ' Dead; 2 )2

    & () E"" Sorted by 0ame ""E

    & ' Dead( 2 )2& (2 E"" Sorted %y Score "?

    & ' Print*t 2

    & 0ame,: ;-

    & TestScore,: ()

    &

  • 7/23/2019 Session 9 Tables and Arrays

    23/23