# 16-matching-2stage-1-1: Two-Stage 1:1 Matching ## 电信业务场景 二级请求书核对。两段式1:1对账处理。第一段:请求书与支付对账→中间文件;第二段:中间文件与调整记录对账→最终结果。 ## Description Tests a two-stage pipeline where the output of the first 1:1 match feeds into a second 1:1 match. Three-way chained matching across FILE-A, FILE-B, and FILE-C using an intermediate TEMP-FILE. - Stage 1: Match FILE-A and FILE-B (1:1), write matched A-side records to TEMP-FILE. Unmatched records (KEY00004 in A, KEY00005 in B) are skipped. - Stage 2: Match TEMP-FILE and FILE-C (1:1), write matched records to FINAL-OUT. Unmatched C records (KEY00006) are skipped. ## Record Layout | Field | Type | Length | Description | |------------|-----------------|--------|---------------------------| | STD-KEY | PIC X | 10 | Record key | | STD-DATA-1 | PIC X | 20 | Description text | | STD-DATA-2 | PIC 9 | 10 | Numeric data (display) | | STD-DATA-3 | PIC S9(7)V99 | 05 | Numeric data (COMP-3) | Total record length: 45 bytes. ## Files | File | Purpose | |-------------------------------|--------------------------------------| | main-16-matching-2stage-1-1.cbl| Main COBOL program (two stages) | | data-gen.sh | Generate three test data files | | run.sh | Compile, run, verify | | README.md | This file | ## Data | File | Records | |-------------|--------------------------------------------------------| | file-a.dat | KEY00001, KEY00002, KEY00003, KEY00004 (unmatched) | | file-b.dat | KEY00001, KEY00002, KEY00003, KEY00005 (unmatched) | | file-c.dat | KEY00001, KEY00002, KEY00003, KEY00006 (unmatched) | The matching chain: KEY00001, KEY00002, KEY00003 exist in all three files and should survive both stages. The unmatched extras are discarded. ## Matching Logic Each stage implements standard 1:1 sorted merge/match: 1. Compare keys from both input streams. 2. If keys are equal, write a matched record and advance both. 3. If keys differ, advance the stream with the smaller key (skip it). 4. Continue until one stream is exhausted. ## Test | Check | Expected | |--------------------------|-----------------------------------| | Stage 1 output (temp) | 3 records x 45 = 135 bytes | | Stage 2 output (final) | 3 records x 45 = 135 bytes | | Matched chain | KEY00001, KEY00002, KEY00003 | | Unmatched filtered | KEY00004, KEY00005, KEY00006 | ## Usage ```bash cd 16-matching-2stage-1-1 bash run.sh ```