Need to see if I can use my read statements to accomplish 3 types of output?

时间秒杀一切 提交于 2019-12-04 20:41:20

I do not think it is possible to do all three tasks with your current code. Also what would happen if the files contained 1,000,000 records. You would be reading a file 1,000,000 times. It would take forever to finish (assuming the hard-disks survived).


For this type of processing I would suggest a sort-merge process:

  • Sort the files into key sequence
  • Do a merge on the 2 files (for 3 files the process is similar, you just have more complicated evaluate).

The processing logic for 2 files in key sequence becomes:

 while not eof
    evaluate true
       when key-file1 < key-file2
         Write file1-record to output-file-2
         read file1
       when key-file1 > key-file2
         Write file2-record to output-file-3
         read file2
       when key-file1 = key-file2
            /* match processing, 
               will involve reading at least one of the files */     
            move key-file1              to hold-key
            while key-file1 =  hold-key
               Write Output-File-1
               read file1
            end
            while key-file2 =  hold-key
               read file2
            end
      end
 end

There will be End-of-file logic to add in

Note: It is unclear from the question how multiple entries for the same key should be handled

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!