我怎样才能在Perl中同时浏览两个文件? [英] How can I walk through two files simultaneously in Perl?

查看:116
本文介绍了我怎样才能在Perl中同时浏览两个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文本文件,其中包含位置 - 值的列数据,按<$ c排序第一个文件的例子(文件 A ) >):

  100 1 
101 1
102 0
103 2
104 1
...

以下是第二个文件(
$ b $ $ $ $ $ $ $ $ $ ...
100 2
101 1
192 3
193 1
...

不是将两个文件中的一个读入哈希表,这个哈希表由于内存限制而过分,我想要做的是同时遍历两个文件,时尚。

这意味着我想通过 A B 并比较 position 值。

如果两个位置相等,然后我执行一个计算与该位置相关的值。否则,如果职位不相等,我移动文件行 A 或文件 B 直到位置相等(当我再次执行我的计算)或者我达到了两个文件的EOF。



有没有办法在Perl中这样做?

解决方案

看起来像一个可能偶然发现的问题,例如数据库表数据与键和值。这是rjp提供的伪代码的实现。

 #!/ usr / bin / perl 

严格使用
使用警告;

sub read_file_line {
my $ fh = shift;

if($ fh and my $ line =< $ fh>){
chomp $ line;
return [split(/ \ t /,$ line)];
}
return;


计算{
#做一些2值的操作
}

open(my $ f1,file1) ;
open(my $ f2,file2);

my $ pair1 = read_file_line($ f1);
my $ pair2 = read_file_line($ f2); $
$ b while($ pair1 and $ pair2){
if($ pair1-> [0]< $ pair2-> [0]){
$ pair1 = read_file_line($ F1);
} elsif($ pair2-> [0]< $ pair1-> [0]){
$ pair2 = read_file_line($ f2);
} else {
compute($ pair1-> [1],$ pair2-> [1]);
$ pair1 = read_file_line($ f1);
$ pair2 = read_file_line($ f2);
}
}

close($ f1);
close($ f2);

希望这有助于您!


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

Here is an example of the first file (file A):

100   1
101   1
102   0
103   2
104   1
...

Here is an example of the second file (B):

20    0
21    0
...
100   2
101   1
192   3
193   1
...

Instead of reading one of the two files into a hash table, which is prohibitive due to memory constraints, what I would like to do is walk through two files simultaneously, in a stepwise fashion.

What this means is that I would like to stream through lines of either A or B and compare position values.

If the two positions are equal, then I perform a calculation on the values associated with that position.

Otherwise, if the positions are not equal, I move through lines of file A or file B until the positions are equal (when I again perform my calculation) or I reach EOF of both files.

Is there a way to do this in Perl?

解决方案

Looks like a problem one would likely stumble upon, for example database table data with keys and values. Here's an implementation of the pseudocode provided by rjp.

#!/usr/bin/perl

use strict;
use warnings;

sub read_file_line {
  my $fh = shift;

  if ($fh and my $line = <$fh>) {
    chomp $line;
    return [ split(/\t/, $line) ];
  }
  return;
}

sub compute {
   # do something with the 2 values
}

open(my $f1, "file1");
open(my $f2, "file2");

my $pair1 = read_file_line($f1);
my $pair2 = read_file_line($f2);

while ($pair1 and $pair2) {
  if ($pair1->[0] < $pair2->[0]) {
    $pair1 = read_file_line($f1);
  } elsif ($pair2->[0] < $pair1->[0]) {
    $pair2 = read_file_line($f2);
  } else {
    compute($pair1->[1], $pair2->[1]);
    $pair1 = read_file_line($f1);
    $pair2 = read_file_line($f2);
  }
}

close($f1);
close($f2);

Hope this helps!

这篇关于我怎样才能在Perl中同时浏览两个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆