title: Printing records out in order using an array
body: <p>I am wondering if what I want to do can be accomplished in COBOL. I am trying to read in hospital and patient data from a file that is not in order. Instead of using Sort (like everyone would in real life), I am to use an array to somehow sort the data that comes in. At the end I'm supposed to print out a report to another file grouped by hospital number.</p>
  
  <p>So, </p>
  
  <pre><code>Hospital #
  patient 018
  patient 020
  
  total for hospital #
  
  Hospital #
  patient 011
  patient 009
  
  total for hospital #
  </code></pre>
  
  <p>All of these hospital numbers is to be between 1 and 30 and patient numbers are between 1 and 20. I have a pretty good idea about what to do and how to sum up patient balances, but what do you think of going about it this way:</p>
  
  <p>Read in data to an array (this would obviously be more than 1 dimension). At this point the data is unsorted in the array. I don't think it matters if it's sorted inside the array as long as it comes out on the report sorted. Does this sound right? And yes, this is for an assignment. Any tips or recommendations, feedback that could be offered without giving the answer?</p>
  
  <p>EDIT:
  Okay, so I tried to create a 2d type array that would have the fields I needed to get the job done (hos num, pat name, pat num, pat amnt). And of course I want to add each patient amount to a running total for hospital subtotal.</p>
  
  <p>Here is my array:</p>
  
  <pre><code>01  HospArray value spaces.
         05  hosnum   occurs 30 times indexed by subsa.
           07  patnum   occurs 20 times indexed by subsb.
             10  patname  PIC X(20).
             10  patamt   PIC 9(7)v99.
           07  hossubtotal  PIC 9(7)v99.
  </code></pre>
  
  <p>While reading in the file and moving it to my working storage fields, I attempt to do two perform varying loops like this:</p>
  
  <pre><code> perform varying subsa from 1 by 1 until subsa &gt; 30
               move hos-num-ws to hosnum (subsa)
            perform varying subsb from 1 by 1 until subsb &gt; 20
  
                 move pat-name-ws to patname(subsa, subsb)
                 move pat-amnt-od-ws to patamt(subsa, subsb)
                 add  patamt(subsa, subsb) to hossubtotal (suba)
  
            end-perform
         end-perform
  </code></pre>
  
  <p>For some reason this won't compile and it gives the error of: Unexpected ADD and I am not sure why. I am using openCobol. I don't have a ton of experience so I'm not sure why this isn't compiling. I am still not sure if I am going in the right direction. I know I want to say, why the count is on a given hospital number move patient data to variables, and then somehow I should know when the current hospital number is done. Output the hospital subtotal, then repeat the process again.</p>