Count numbers of lines under each header

OS/VS COBOL, COBOL II, Enterprise COBOL for z/OS. OpenCOBOL and OOCobol.
Previous topicNext topic

Topic author
Puneeth R

Count numbers of lines under each header

Post by Puneeth R » Fri May 20, 2016 10:30 pm

Hi,
I have a requirement as follows,
I get an input file which has header followed by many lines (detail records).N I need to find the number of lines under each header and write it to the header. What approach should I take? I mean which is the best way to achieve this? COBOL or any sort techinques? Please give me some start about the approach to be taken.
Following is the sample input file that I get,

Code: Select all

HD1234abcdefg
Ln1
Ln2
Ln3
HD1457789
Ln1
Ln2
I need to count the number of lines under each header and write it to its header record.
Last edited by Anuj Dhawan on Sat May 21, 2016 5:43 pm, edited 2 times in total.
Reason: Added code tags and moved the topic to COBOL part from 'You are a Guest'.



User avatar

Robert Sample
Global Moderator
Global Moderator
Posts: 1287
Joined: Fri Jun 28, 2013 1:22 am
Location: Dubuque Iowa

Re: Count numbers of lines under each header

Post by Robert Sample » Fri May 20, 2016 11:41 pm

You left out some information -- how many lines per header maximum? record length? fixed or variable length records?
The COBOL approach would be to read each record into a table until the next header is read.  The number of table entries (minus 1 for the header) would give you the count to put into the header.  Then write all the records from the table to your output data set, initialize your table, move the header to the first table entry, and start filling the table again.




Puneethr
New Member
Posts: 5
Joined: Fri May 20, 2016 10:34 pm

Re: Count numbers of lines under each header

Post by Puneethr » Sat May 21, 2016 8:51 am

Hi Robert,
Thanks a lot for your response. And the file is fixed block with record length 2024. Maximum headers and lines are not known...according to me there might be 1000 headers in a file and 100lines under each header at the max.
I have never used tables or arrays till now. I will try and follow the method suggested by you and will post here if I get into trouble. Any more suggestions regarding the table declarations? As to how much should I declare? It should be a two dimensional array rite? Sorry for asking silly doubts. But I have never worked with arrays.
Thanks in advance.



User avatar

Robert Sample
Global Moderator
Global Moderator
Posts: 1287
Joined: Fri Jun 28, 2013 1:22 am
Location: Dubuque Iowa

Re: Count numbers of lines under each header

Post by Robert Sample » Sat May 21, 2016 10:07 am

Be generous in your table declarations -- if you guess small, you'll have to rerun the compile and link (bind) before rerunning the program; guessing too big merely means your program has unused memory.  And depending upon the version of COBOL you have anywhere from 128 MB (version 3.4) to 1 GB (version 6.1 - technically, 999,999,999 bytes is not quite 1 GB) of memory available, so define your table something like

Code: Select all

01  HOLD-TABLE.
    05  HT-ENTRY            OCCURS 5000
                            INDEXED BY HT-INDEX
                                       HT-WRITE-INDEX
                            PIC X(2024).
Read the first record and move it to the first table entry, setting HT-INDEX to 1.  After that, start your read loop.  In the read loop, if the record is NOT a header, add 1 to HT-INDEX (SET HT-INDEX UP BY 1) and move the input data to HT-ENTRY (HT-INDEX), then read the next record.  If the record IS a header, write the table out (see below), set HT-INDEX to 1, and move the record to HT-ENTRY (HT-INDEX).  When you hit end-of-file, you'll need to write out the table because the last group of records in the file is still in the table.  I did not include the code to validate that you don't run out of table entries, but you'll want to check for that since you don't know how many records can occur for a header.
Your code to write the table will be something like

Code: Select all

PERFORM
    VARYING HT-WRITE-INDEX FROM 1 BY 1
      UNTIL HT-WRITE-INDEX > HT-INDEX
    WRITE OUT-FILE-RECORD FROM HT-ENTRY (HT-WRITE-INDEX)
END-PERFORM
If you use index as I show, you'll need to use SET instead of ADD / MOVE (which are used for subscripts).
I would count the input records, the input headers, the maximum number of records for a header, the output records and print these numbers out at the end of the program to get a better idea of what the data set has.




enrico-sorichetti
Global Moderator
Global Moderator
Posts: 834
Joined: Wed Sep 11, 2013 3:57 pm

Re: Count numbers of lines under each header

Post by enrico-sorichetti » Sat May 21, 2016 11:14 am

I would read the input file using two ddnames
the first ddname ( readonly ) to find the counts the second to write the updates


cheers
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort 8-)


William Collins
Global Moderator
Global Moderator
Posts: 499
Joined: Sun Aug 25, 2013 7:24 pm

Re: Count numbers of lines under each header

Post by William Collins » Sat May 21, 2016 1:08 pm

enrico's idea is a flyer for this, but bear in mind that it is not a panacea for all "we should have thought of that when we designed the file" situations.

So, why is the count needed? Why can't it be put on when the file is created?




Puneethr
New Member
Posts: 5
Joined: Fri May 20, 2016 10:34 pm

Re: Count numbers of lines under each header

Post by Puneethr » Sat May 21, 2016 3:47 pm

Hi Robert,
Thanks again.But this will only write the input records to output right? but I want to put the line count to header. As you said the (number of table entries - 1) would give the line count, but how to update the header before writing? Please let me know your thoughts.




Puneethr
New Member
Posts: 5
Joined: Fri May 20, 2016 10:34 pm

Re: Count numbers of lines under each header

Post by Puneethr » Sat May 21, 2016 3:50 pm

Hi Enrico,
Thanks for your reply.Your idea too is great but the only drawback is it will cause more load, as now i will have to read the file twice.This can be kept as a backup option if the table approach doesnt work. Thank you for your suggestion,.




Puneethr
New Member
Posts: 5
Joined: Fri May 20, 2016 10:34 pm

Re: Count numbers of lines under each header

Post by Puneethr » Sat May 21, 2016 3:54 pm

William Collins wrote:enrico's idea is a flyer for this, but bear in mind that it is not a panacea for all "we should have thought of that when we designed the file" situations.

So, why is the count needed? Why can't it be put on when the file is created?
Hi William,
The file is an existing one and the structure cannot be altered is what i found out.So trying to calculate the line count under each header by writing a new program.
Puneeth



User avatar

Robert Sample
Global Moderator
Global Moderator
Posts: 1287
Joined: Fri Jun 28, 2013 1:22 am
Location: Dubuque Iowa

Re: Count numbers of lines under each header

Post by Robert Sample » Sat May 21, 2016 6:56 pm

The file is an existing one and the structure cannot be altered is what i found out.
I hope you mean that the existing processing cannot be altered.  If you cannot change the header record to add your count, then this
I need to count the number of lines under each header and write it to its header record.
from your original post is a contradiction.  Terminology is critical in IT since similar terms may mean very different things, and you need to be able to precisely state what is needed to be able to code a program correctly.




enrico-sorichetti
Global Moderator
Global Moderator
Posts: 834
Joined: Wed Sep 11, 2013 3:57 pm

Re: Count numbers of lines under each header

Post by enrico-sorichetti » Sat May 21, 2016 6:59 pm

Your idea too is great but the only drawback is it will cause more load,
computer time costs less that programmer' s time

to test it just write a 10 liner program


cheers
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort 8-)

User avatar

Anuj Dhawan
Founder
Posts: 2413
Joined: Sun Apr 21, 2013 7:40 pm
Location: Mumbai, India

Re: Count numbers of lines under each header

Post by Anuj Dhawan » Sat May 21, 2016 7:34 pm

I have been on a non-mainframe side of activities for some time now and that keeps me on the boat with no mainframe access until I get a working Hercules. Tried a guess work for a SORT solution, see if this works: (it places the count for detail records on position 15 in header)

Code: Select all

//STEP01 EXEC PGM=SORT
//SYSOUT DD SYSOUT=*
//SORTIN DD DSN=...  input file (FB/2024)
//SYMBL DD DSN=&&SYMBL1,UNIT=SYSDA,SPACE=(TRK,(1,2)),DISP=(,PASS)
//TEMP1 DD DSN=&&TEMP1,UNIT=SYSDA,SPACE=(CYL,(5,10)),DISP=(,PASS)
//SYSIN DD *
  OPTION COPY
  OUTFIL FNAMES=SYMBL,REMOVECC,NODETAIL,
    BUILD=(1,80),
    TRAILER1=('DTLCNT,''',COUNT-1=(EDIT=(TTTTTT)),'''')
  OUTFIL FNAMES=TEMP1,
    OVERLAY=(2025:SEQNUM,8,ZD)
/*
//STEP02 EXEC PGM=SORT
//SYMNAMES DD DSN=&&SYMBL1,DISP=(OLD,PASS)
//SYSOUT DD SYSOUT=*
//SORTIN DD DSN=&&TEMP1,DISP=(OLD,PASS)
//SORTOUT DD DSN=...  output file (FB/2024)
//SYSIN DD *
  OPTION COPY
  INREC IFOUTLEN=2024,
    IFTHEN=(WHEN=(2025,8,ZD,EQ,1),OVERLAY=(15:DTLCNT))
/*
I'm doubtful about the multiple headers though - William might put me in order, I'd believe.


Thanks,
Anuj


Puneethr
New Member
Posts: 5
Joined: Fri May 20, 2016 10:34 pm

Re: Count numbers of lines under each header

Post by Puneethr » Sat May 21, 2016 9:21 pm

Hello Robert, Anuj, William and Enrico,

Thanks a lot to each one of you. With all your inputs I was able to write a Cobol program for the requirement.
Highly appreciate the way you guys are helping beginners like me.
@Anuj: I am amazed to know that this can be achieved using sort also.Will give that a try as well.
@William: Sure, I will keep in mind that I have to use the correct terminology while mentioning my requirement. Will follow this from next time.

Thanks again guys!



User avatar

Anuj Dhawan
Founder
Posts: 2413
Joined: Sun Apr 21, 2013 7:40 pm
Location: Mumbai, India

Re: Count numbers of lines under each header

Post by Anuj Dhawan » Sat May 21, 2016 10:02 pm

COBOL or SORT - Keep us posted about the progress you make at your end on this! :)


Thanks,
Anuj

Previous topicNext topic

Return to “IBM COBOL, GnuCOBOL (OpenCOBOL), OOCobol.”