How can I walk through two files simultaneously in Perl?

前端 未结 4 444
滥情空心
滥情空心 2020-12-31 18:36

I have two text files that contain columnar data of the variety position-value, sorted by position.

Here is an example of the

4条回答
  •  一向
    一向 (楼主)
    2020-12-31 19:19

    If the files are sorted, step through them based on which one has the lower position.

    Pseudocode:

    read Apos, Aval from A # initial values
    read Bpos, Bval from B 
    until eof(A) or eof(B)
      if Apos == Bpos then
        compare()
        read Apos, Aval from A # advance both files to get a new position
        read Bpos, Bval from B
      fi
      if Apos < Bpos then read Apos, Aval from A
      if Bpos < Apos then read Bpos, Bval from B
    end
    

    You could also use join(1) to isolate the lines with common positions and process that at your leisure.

提交回复
热议问题