codevelocity
codevelocity

Reputation: 1

How to read two records and compare them with a primary key in sequential file system?

This is the record format for my input file:

ID NAME Purchaseamount month
1  xxx  10000           feb
1  xxx  10000            mar
1  xxx  10000           apr
2  yyy  100              jan
2  yyy  2054             mar

How can I add the purchase amount for every person? I will give you the pseudo-code of my work.

Read inputfile,
move to working storage variables,
end read,
perform until eof,
read input file,
if inputid = ws-input id,
add purchase amount,
else write to output file

Upvotes: 0

Views: 8545

Answers (3)

Valdis Grinbergs
Valdis Grinbergs

Reputation: 453

Bruce provides a good solution, but there is often more than one way to do things. Here is an alternate solution (in pseudocode) that is inspired by Michael A. Jackson's "Principles of Program Design" (I recommend it if you have not read it).

OPEN INPUT INPUT-FILE
READ INPUT-FILE 
PERFORM PROCESS-PERSON UNTIL INPUT-FILE-EOF
CLOSE INPUT-FILE
STOP RUN
.

PROCESS-PERSON.
    MOVE INPUT-ID TO LAST-INPUT-ID
    MOVE ZERO TO PURCHASE-TOTAL
    PERFORM PROCESS-RECORD UNTIL INPUT-FILE-EOF OR
        (INPUT-ID NOT = LAST-INPUT-ID)
    WRITE PERSON-TOTAL
    .

PROCESS-RECORD.
    ADD PURCHASE-AMOUNT TO PURCHASE-TOTAL
    READ INPUT-FILE
    .

Upvotes: 2

Joe Zitzelberger
Joe Zitzelberger

Reputation: 4263

Sometimes, the best program is one you don't have to write.

This really is a problem that begs for a utility tool solution. Syncsort will do match and sums type things, so will other tools like FileAid and FileManager. You could probably knock that out with two or three lines of config information.

Upvotes: 1

Bruce Martin
Bruce Martin

Reputation: 10543

for pseudo code Try:

 move 0            to purchaseTotal
 Read InputFile
 move inputId      to lastInputId
 Perform until eof
     if inputId = lastInputId
        add purshaseAmount     to purchaseTotal
     else
        write personTotal
        move 0                 to purchaseTotal 
        move inputId      to lastInputId
     end-if  
     Read InputFile
end-Perform
write personTotal

do you have any code and the result ???

Upvotes: 1

Related Questions