Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 653655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:24:23+00:00 2026-05-13T22:24:23+00:00

I would like informations to manipulate tables. I encounter few problem with a piece

  • 0

I would like informations to manipulate tables.
I encounter few problem with a piece of cobol code like below:

01 TABLE-1.  
    05 STRUCT-1 OCCURS 25 TIMES.
        10 VALUE-1 PIC AAA.  
        10 VALUE-2 PIC 9(5)V999.  
    05 NUMBER-OF-OCCURS PIC 99.

How do you update values? (update a VALUE-2 when you know a VALUE-1)
How look up a value and add new one?
Thanks a lot!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T22:24:23+00:00Added an answer on May 13, 2026 at 10:24 pm

    How to look up a value/How to update a value

    First you have to look up the record (row) that you want to update. This is typically done by searching the table for
    a given key value. COBOL provides a couple of ways to do this. I recommend that you start by
    reviewing the COBOL SEARCH
    statement. If STRUCT-1 records are sorted, you could use SEARCH ALL, otherwise you must use SEARCH or just code
    your own search loop. In order to use any of these techniques you will need to declare another variable somewhere
    in your program to use as an index (offset) into the STRUCT-1 table. COBOL provides the INDEXED BY phrase on the
    OCCURS clause to declare an index specific to the given table
    (see OCCURS)

    Once you have set the index into STRUCT-1 to point to the row to be updated you just MOVE the
    value to the appropriate variables within that row, for example

    MOVE 123.456 TO VALUE-2 (IDX-1)

    where IDX-1 is the index referred to above. Note that you can use either an integer or
    index variable
    to specify the row number to be updated, you are not limited to using an INDEX type variable. However,
    it is generally more efficient to use INDEX variables over other types of variables, particularily
    when working with multi-dimensional tables where the program makes lots of references to the table.

    How to add a new row

    First recognize that STRUCT-1 contains exactly 25 rows. COBOL does not have a mechanism to dynamically
    increase or decrease this number (I’ve heard this will possible in the next ISO COBOL standard – but don’t
    hold your breath waiting for it). Technically all 25
    rows are available at any time. However a common convention is to ‘grow’ a table from being empty
    to full sequentially, one row at a time. To use this convention you need to assign a variable to
    keep track of the last used row number (don’t forget to initialize this variable to zero at program startup).
    In your example it looks like the variable NUMBER-OF-OCCURS does this job
    (I didn’t mention it but, you need this variable to bound the SEARCH discussed above).

    To ‘add’ a row, just increment NUMBER-OF-OCCURS by 1. Be careful not to exceed the table size. Example
    code might be:

    IF NUMBER-OF-OCCURS < (LENGTH OF TABLE-1 / LENGTH OF STRUCT-1 (1))
       ADD +1 TO NUMBER-OF-OCCURS
    ELSE
       table is full, preform some error/recovery routine
    END-IF
    

    The above code avoids the explicit use of the number of occurs in TABLE-1 which in turn can save
    a number of maintenance problems when/if the number of OCCURS is ever changed.

    See the NOTE at the bottom: There is a really big Woops here – did you catch it!

    Now back to the search problem. The following code example illustrates how you might
    proceed:

    WORKING-STORAGE Declaration:

     01 FOUND-IND  PIC X(1).
        88 FOUND-YES  VALUE 'Y'.
        88 FOUND-NO   VALUE 'N'.
     77 MAX-IDX   USAGE IS INDEX.
    
     01 TABLE-1.
        05 STRUCT-1 OCCURS 25 TIMES INDEXED BY IDX-1.
           10 VALUE-1 PIC AAA.
           10 VALUE-2 PIC 9(5)V999.
        05 NUMBER-OF-OCCURS PIC 99.
    

    What was added:

    • FOUND-IND is used to indicate whether the row you are looking for has been found. The 88 levels give specific values to set/test
    • MAX-IDX is used to set an upper bound limit on the search. You could use NUMBER-OF-OCCURS in the upper bounds test but this would force a data type converson on every test which isn’t very efficient
    • IDX-1 is used as the index (offset) into the STRUCT-1 table.

    Personally, I would declare NUMBER-OF-OCCURS as PIC S9(4) BINARY but what you have will work.

    Assuming that STRUCT-1 is not sorted and NUMBER-OF-OCCURS represents the current
    number of active rows in STRUCT-1 this
    is an example of how you might code the SEARCH when looking for the value ‘ABC’:

    SET FOUND-NO TO TRUE
    IF NUMBER-OF-OCCURS > ZERO
    
       SET IDX-1 TO 1
       SET MAX-IDX TO NUMBER-OF-OCCURS
    
       SEARCH STRUCT-1
         WHEN IDX-1 > MAX-IDX
           CONTINUE
         WHEN VALUE-1 (IDX-1) = 'ABC'
           SET FOUND-YES TO TRUE
       END-SEARCH
    END-IF
    
    IF FOUND-YES
       row found, use IDX-1 to reference the row containing 'ABC'
    ELSE
       row not found, IDX-1 does not contain a valid index
    END-IF
    

    How it works:

    • Start by assuming the row is not in the table by setting FOUND-NO to true.
    • The first IF ensures that there is at least 1 active row in STRUCT-1 before beginning the search (it is an error to set an INDEX to zero – so you need to guard against that).
    • The SEARCH terminates when the first SEARCH WHEN clause is satisified. That is why the ‘do nothing’ verb CONTINUE can be used when we run out of rows to search. The other terminating condition (finding the value you are looking for) is the only place where FOUND-YES can be set.
    • When the SEARCH completes, test for success or failure then act accordingly.

    Some exercises for you to research:

    • Why did I not have to code an AT END clause in the SEARCH statement?
    • Why did I not have to code a VARYING clause in the SEARCH statement?
    • Why did I code the WHERE clauses in the order that I did?

    Hope this gets you started down the right path.

    Edit

    In response to your question in the comments: Could we use NUMBER-OF-OCCURS as index for the search. The
    answer is yes, but you need to implement some different rules. When using NUMBER-OF-OCCURS
    as an index you can no longer use it to keep track of how many rows currently contain
    valid data. This means you need another mechanism to identify unused rows in STRUCT-1.
    This might be accomplished by initializing un-used rows with a sentinal value (eg. LOW-VALUE) that you
    will never actually want to put into the table. The SEARCH becomes:

    SET FOUND-NO TO TRUE 
    MOVE 1 TO NUMBER-OF-OCCURS 
    SEARCH STRUCT-1 VARYING NUMBER-OF-OCCURS
      WHEN VALUE-1 (NUMBER-OF-OCCURS) = 'ABC' 
        SET FOUND-YES TO TRUE 
    END-SEARCH 
    

    The above will search every row in STRUCT-1 in the event that the value you are searching for
    (ie. ABC) is not in the table. As an optimization you can add a second WHEN clause to terminate the
    search upon finding a sentinal value:

    WHEN VALUE-1 (NUMBER-OF-OCCURS) = LOW-VALUE
       CONTINUE
    

    The above assumes LOW-VALUE was used to identify unused rows. You can also drop IDX-1 and MAX-IDX
    from your working storage since this solution doesn’t need them.

    Using NUMBER-OF-OCCURS as an index also means you must change the way you search for an empty row
    to insert a new value. The easiest way to do this is to search the table using the above
    code for LOW-VALUE instead of 'ABC'. If FOUND-YES has been set at the end of the search, then
    NUMBER-OF-OCCURS is the index of the first unused row. If FOUND-NO has been set, then the table is
    already full.

    The above code is a lot simpler than what I initially suggested. So why did I give you the more
    complicated solution? The more complicated solution is more efficient because it makes many
    fewer internal offset calculations and data type conversions when running through the table.
    It also avoids doing an additional
    SEARCH to find the next unused row. These efficiencies
    may not be of concern in your application, but if the tables are large and accessed frequently you
    should be aware of the performance aspect of searching tables and forced data type conversions (for
    example the cost of converting a PIC 99 field into an index reference).

    Note:

    My original example to calculate whether the table was full using the LENGTH OF special register
    would work in this example but has a really bad built in assumption! The LENGTH OF TABLE-1 includes
    not only the STRUCT-1 table but the NUMBER-OF-OCCURS too. The length of NUMBER-OF-OCCURS is less than one
    occurance of STRUCT-1 so it all works out ok due to truncation of the result into an integer value.
    This is a very good example of code working correctly for the wrong reason! To make the proper calculation
    you would have to adjust your working storage to something like:

    01 TABLE-1.
       05 STRUCT-TABLE. 
          10 STRUCT-1 OCCURS 25 TIMES.
             20 VALUE-1 PIC AAA. 
             20 VALUE-2 PIC 9(5)V999. 
       05 NUMBER-OF-OCCURS PIC 99. 
    

    and the bounds calculation would become:

    IF NUMBER-OF-OCCURS < (LENGTH OF STRUCT-TABLE / LENGTH OF STRUCT-1 (1)) 
       ADD +1 TO NUMBER-OF-OCCURS 
    ELSE 
       table is full, preform some error/recovery routine 
    END-IF 
    

    Or you could just move NUMBER-OF-OCCURS out of the TABLE-1 record definition.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like information on algorithms that can help identify commonality and differences between
I would like to have information about the icons which are displayed alongside the
I would like to access information on the logical drives on my computer using
I would like to get up-to-date information on Google's index of a website, and
I would like to monitor the following system information in Java: Current CPU usage**
I would like to remove the domain/computer information from a login id in C#.
Would like to get a list of advantages and disadvantages of using Stored Procedures.
Would like to create a strong password in C++. Any suggestions? I assume it
I would like to test a string containing a path to a file for
I would like to sort an array in ascending order using C/C++ . The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.