Cobol Tables Introduction

download Cobol Tables Introduction

of 23

Transcript of Cobol Tables Introduction

  • 8/10/2019 Cobol Tables Introduction

    1/23

    Tables 1

    TABLES () group of storage locations (i.e. elements) with the same name accessed by name and subscript ex. var-name (sub) similar to arrays in other languages

  • 8/10/2019 Cobol Tables Introduction

    2/23

  • 8/10/2019 Cobol Tables Introduction

    3/23

  • 8/10/2019 Cobol Tables Introduction

    4/23

    Tables 4

    TABLE examplesIdentifier used with OCCURS may also be group item

    01 Tax-Table.05 Group-X Occurs 20 Times.

    10 City Pic X(6).10 Tax-Rate Pic V999.

  • 8/10/2019 Cobol Tables Introduction

    5/23

    Tables 5

    EXAMPLE #101 DAYS-OF-MONTH-TABLE.

    03 MONTHS OCCURS 12 times.05 Month-Name PIC X(15).05 Month-Num-Days PIC 99.

    Move January to Month -Name (1)Move 31 to Month-Num-Days (1)

    EXAMPLE #2 01 DAYS-OF-MONTH-TABLE.

    05 Month-Name PIC X(15) OCCURS 12 TIMES.

    05 Month-Num-Days PIC 99 OCCURS 12 TIMES.Move January to Month -Name (1)Move 31 to Month-Num-Days (1)

    More TABLE examples

    How else initialize

    Month-Name andMonth-Num-Days?!

  • 8/10/2019 Cobol Tables Introduction

    6/23

    Tables 6

    01 GRADE-TABLE.05 QUIZ-TABLE OCCURS 40 TIMES.

    10 QUIZ1 PIC 9(2).10 QUIZ2 PIC 9(2).10 QUIZ3 PIC 9(2).10 QUIZ4 PIC 9(2).

    How do we average all the quiz scores for QUIZ4? Compute-Average-Quiz4.

    MOVE ZERO TO Total-GradeMOVE 1 TO SubPERFORM UNTIL Sub > Num-Quizzes

    ADD QUIZ4(Sub) TO Total-Grade ADD 1 TO SubEND-PERFORMDIVIDE Total-Grade BY Num-Quizzes

    GIVING Average-Score

    One More TABLE example

    ***Convert toPerform-Varying

  • 8/10/2019 Cobol Tables Introduction

    7/23

    Tables 7

    Two ways to use VALUE clause to initialize all elements to zero:01 Array-1 Value Zero.

    05 Totals Occurs 50 Times Pic 9(5).01 Array-1.

    05 Totals Occurs 50 Times Pic 9(5) Value Zero.

    Can also initialize each element to different value

    01 Day-Names Value 'SUNMONTUEWEDTHUFRISAT'.05 Days Occurs 7 Times Pic X(3).??? Can an initialization be done on the 05 directly above???

    INITIALIZE STATEMENT (i.e. in Procedure Division)

    A series of elementary items contained within a group item can all beinitialized. Numeric items will be initialized to zero and nonnumericitems will be initialized to blank.

    FORMAT: INITIALIZE {identifier- 1}

    INITIALIZING TABLES

  • 8/10/2019 Cobol Tables Introduction

    8/23

    Tables 8

    REDEFINESPurpose : Used to define a field with a value clause then redefine it as an array.However, you cannot do the reverse; that is, once an entry has been defined byan OCCURS clause, it may NOT be redefined.

    FORM T in Data Division :L# identifier-1 REDEFINES identifier-2 [finish declaration]

    where:L# means level number

    Identifier-1 and identifier-2 must be on the same level

    EX MPLES:See table example program

    01 Rates.

    05 10Rate PIC 99V999 value 12.345.05 100Rate REDEFINES 10Rate PIC 999V99.05 1000Rate REDEFINES 10Rate PIC 9999V9.

    Result 100Rate = 123.45 and 1000Rate = 1234.5

    01 HoldDate.02 amerDate.

    03 amerDay PIC 99 value 11.03 amerMonth PIC 99 value 3.03 amerYear PIC 9(4) value

    2014

    CanDate REDEFINES AmerDate.03 CanMonth PIC 99.03 CanDay PIC 99.03 CanYear PIC 9(4).

    canMonth = 3 and CanDay = 11

  • 8/10/2019 Cobol Tables Introduction

    9/23

    Tables 9

    Define array as follows:01 Temperature-Array.

    05 Day-In-Week Occurs 7 Times.10 Hour Occurs 24 Times.

    15 Temp Pic 9(3).

    2 dimensional TABLES (ex#1)For your information :If reading in from file, what does input filelook like?!

    FD input file01 input-rec pic 999.

    Find the average temp for: each day.

    Find the average temp for each hour.

    PARTICIPATION POINTS (5)

    (due in one week graded on if works!)

    What if have negative temperatures?How solve?! That is, what does input filelook like? What does declaration looklike?

    Perform a from 1 by 1 until a > 7

    perform b from 1 by 1 until b > 24

    read input-file at end..

    move input-rec to temp (a b)

    end-perform

    End-perform

  • 8/10/2019 Cobol Tables Introduction

    10/23

    Tables 10

    2 dimensional TABLES (ex#2)01 Jeans-Table.

    05 Jean-Gender OCCURS 2 TIMES.10 Jean-Color OCCURS 3 TIMES.

    15 Jean-SalesValue PIC 9(8)V99.15 Jean-NumSold PIC 9(7).

  • 8/10/2019 Cobol Tables Introduction

    11/23

    Tables 11

    2 dimensional TABLES (ex#3)

    01 Jeans-Table.

    05 Jean-Size OCCURS 10 TIMES.10 Exact-Size PIC 99.10 Jean-Style PIC X.10 Jean-Color OCCURS 3 TIMES.

    15 Jean-SalesValue PIC 9(8)V99.15 Jean-NumSold PIC 9(7).

  • 8/10/2019 Cobol Tables Introduction

    12/23

    REDEFINES

    01 MONTH-NAMES.

    05 STRING-1 PIC X(15)

    VALUE JANFEBMARAPRMAY.

    05 MONTH REDEFINES STRING-1

    OCCURS 5 TIMES PIC XXX.

    MOVE MONTH(3) TO MONTH-OUT

  • 8/10/2019 Cobol Tables Introduction

    13/23

  • 8/10/2019 Cobol Tables Introduction

    14/23

    Referencing a Table01 TABLE.

    05 TABLE-ENTRY OCCURS 10 TIMES.10 NUM PIC 99.10 NAME PIC X(30).10 ITEM PIC X(5) OCCURS 3 TIMES.

    COBOL tables are 1-indexedSome valid References with subscripts:

    TABLE-ENTRY(SUB)

    TABLE NUM(SUB) NAME(SUB)

    ITEM(SUB1,SUB2)

  • 8/10/2019 Cobol Tables Introduction

    15/23

    Subscripts vs Indexes Subscripts are defined separately from the table

    definitions.01 MYTABLE.

    05 ITEM PIC X(3) OCCURS 10 TIMES.

    01 I PIC 9(4) BINARY....

    MOVE 1 TO I

    MOVE ABC TO ITEM(I)

    Subscripts are numeric fields choose BINARY fields forefficiency, although packed and zoned fields also work

  • 8/10/2019 Cobol Tables Introduction

    16/23

    Subscripts vs Indexes

    Subscripts can be easily printed01 MYTABLE.

    05 ITEM PIC X(3) OCCURS 10 TIMES.

    01 I PIC 9(4) BINARY.

    ...

    MOVE 1 TO I

    MOVE ABC TO ITEM(I)

    DISPLAY I

  • 8/10/2019 Cobol Tables Introduction

    17/23

    Subscripts vs Indexes

    Subscripts represent an occurrence number, 1 isthe first occurrence, 2 is the second,

    01 MYTABLE.

    05 ITEM PIC X(3) OCCURS 10 TIMES.

    01 I PIC 9(4) BINARY.

    ...

    PERFORM VARYING I FROM 1 BY 1 UNTIL I > 10

    DISPLAY ITEM(I)END-PERFORM

  • 8/10/2019 Cobol Tables Introduction

    18/23

    Subscript Program

  • 8/10/2019 Cobol Tables Introduction

    19/23

    Subscripts vs Indexes Indexes are created when a table is defined

    01 MYTABLE.

    10 LETTERVALS PIC X(10) VALUE 'ABCDEFGHIJ'.

    10 LETTER REDEFINES LETTERVALSPIC X(1) OCCURS 10 TIMES

    INDEXED BY I.

    Indexes are manipulated with SET statements andautomatically altered by PERFORM statements

    SET I TO 1

    MOVE LETTER(I) TO CHAROUT

  • 8/10/2019 Cobol Tables Introduction

    20/23

    Subscripts vs Indexes

    Indexes are generally more efficient than subscripts

    Indexes represent offsets from the beginning of the tableSET I TO 1

    Causes I to have the binary value 0 internally.

    It takes a bit of work to print them

  • 8/10/2019 Cobol Tables Introduction

    21/23

    Index Program

  • 8/10/2019 Cobol Tables Introduction

    22/23

    Tables with Two Dimensions01 TABLE.

    05 TABLE-ENTRY OCCURS 10 TIMESINDEXED BY SUB1.

    10 NUM PIC 99.10 NAME PIC X(30).

    10 ITEM PIC X(5) OCCURS 3 TIMESINDEXED BY SUB2.

    Valid References with subscripts:TABLE-ENTRY(SUB1)

    TABLE NUM(SUB1) NAME(SUB1)

    ITEM(SUB1,SUB2)

  • 8/10/2019 Cobol Tables Introduction

    23/23

    Subscripts and Indexes

    In a two-dimensional table, the two subscriptscorrespond to the row and column numbers.

    In a three-dimensional table, the threesubscripts correspond to the depth, row, andcolumn numbers.

    Indexes use address computation to efficientlyreference items in a table.