Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Perl将一个文件的列替换为另一个文件的列?_Perl - Fatal编程技术网

如何使用Perl将一个文件的列替换为另一个文件的列?

如何使用Perl将一个文件的列替换为另一个文件的列?,perl,Perl,假设文件1有两列,如下所示: fuzz n. flowering shrub of the rhododendron family dyspeptic adj. bright blue, as of the sky dysplexi adj. of Byzantium or the E Roman Empire eyrie adj. of the Czech Republic or Bohemia azalea adj. su

假设文件1有两列,如下所示:

fuzz n. flowering shrub of the rhododendron family dyspeptic adj. bright blue, as of the sky dysplexi adj. of Byzantium or the E Roman Empire eyrie adj. of the Czech Republic or Bohemia azalea adj. suffering from dyslexia Czech adj. suffering from dyspepsia Byzantine n. eagle's nest azure n. mass of soft light particle azalea azure Byzantine Czech dyslexic dyspeptic eyrie fuzz 文件2只有一个束,看起来像:

fuzz n. flowering shrub of the rhododendron family dyspeptic adj. bright blue, as of the sky dysplexi adj. of Byzantium or the E Roman Empire eyrie adj. of the Czech Republic or Bohemia azalea adj. suffering from dyslexia Czech adj. suffering from dyspepsia Byzantine n. eagle's nest azure n. mass of soft light particle azalea azure Byzantine Czech dyslexic dyspeptic eyrie fuzz 我想用文件2的列替换文件1的第一列。因此,文件3应如下所示:

azalea n. flowering shrub of the rhododendron family azure adj. bright blue, as of the sky Byzantine adj. of Byzantium or the E Roman Empire Czech adj. of the Czech Republic or Bohemia dyslexic adj. suffering from dyslexia dyspeptic adj. suffering from dyspepsia eyrie n. eagle's nest fuzz n. mass of soft light particle
open my $fh, ...
我有一种感觉,做这类工作有一种或另一种简单的方法,很可能有一些方便的模块,但现在我根本不能用最低效的方法来做。我试过一堆代码,比如

while<$line1 = file1>{
while<$line2 = file2>{
join $line,$line2 

但是一点运气都没有。有人能帮我指出正确的方向吗?像往常一样,感谢您的指导。

我读了这篇文章,因为您想输出第一个文件,并像第二个文件一样排序。重读之后,您似乎只想替换列,而不改变顺序。假设您可以处理打开文件的问题,下面是解决方案

while(($line1 = <FILE1>) && ($line2 =  <FILE2>)){
  chomp $line2;
  $line1 =~ s/^\w+/$line2/;
  print FILE3 $line1;
}
这是我最初的解决方案,按照条目在第二个文件中出现的顺序进行排序

创建文件1的哈希

$dictionary = {}
while (<FILE1>){
  m/^(\w+)\s+(.*)$/;
  $dictionary{$1}=$2;
}
在文件2中查找每个键的定义并打印一条连接线

while (<FILE2>){     
  $key =~ s/\s*//g;
  print FILE3 "$key\t\t$dictionary{$key}\n";
}

如果要同时阅读两行,请尝试以下操作:

while(defined(my $line1 = <file1>)
      and defined(my $line2 = <file2>)) {
  # replace contents in $line1 with $line2 and do something with $line1
}
当然,在现代Perl中,您可以将文件句柄存储在词汇范围内的变量中,如下所示:

azalea n. flowering shrub of the rhododendron family azure adj. bright blue, as of the sky Byzantine adj. of Byzantium or the E Roman Empire Czech adj. of the Czech Republic or Bohemia dyslexic adj. suffering from dyslexia dyspeptic adj. suffering from dyspepsia eyrie n. eagle's nest fuzz n. mass of soft light particle
open my $fh, ...

然后将丑陋的全局替换为良好的词汇范围。这样做更好,而且它可以让你以小步骤思考你想做什么

从每个文件中读取一行。 文件1有两列,因此将其拆分为两列。 现在,文件1中的一行分为两部分,文件2中的一行。 打印要保留的部分:文件1的第一部分和文件2中的部分。 然后继续这样做,直到一个文件或另一个文件的行用完为止

以下是您需要的一些物品:


打开一个文件:openmy$filehandle,“你可以使用cut-c10-file1 | paste file2-on*nix。

基于他问题中可怕的while-loop-ish内容,我认为OP在语法上是混乱的。但是好东西。@Chris,是的,是的,我的Perl语法很糟糕。我知道。这种情况经常发生,我就是不能使用正确的语法来实现我的想法。@Ether,谢谢你一步一步的建议。我认为我需要提高我的语法知识。@Mike我强烈建议你掌握一下学习Perl的llama书籍-它非常有助于学习基础知识,并向你展示在哪里查找所有难以记住的细节。@Ether,我实际上正在阅读llama书籍,我几乎完成了。不过还是要谢谢你:这个脱衣舞功能在哪里?它在下一行。暂时忘记Perl没有条带。@EmFi,谢谢分享代码。我刚刚添加了安装程序并测试了代码,但它似乎不起作用。Perl错误表示无法修改标量赋值中的逻辑and&。@EmFi,Perl还报告此行$line2~=s/^\w+/$line1/;有错误。所以这一行$line2~=s/^\w+/$line1/将用选中的$line1替换开头的单词?但是语法似乎有问题。@Chris,知道如何同时读两行是件好事。为什么学习Perl这本书没有提到这行代码?有什么要说的吗?你真的不能推断吗?这有点毛茸茸的,而且很少见。如果您的文件非常小,那么您可以很容易地将它们读入数组并在内存中使用它们。但是,如果希望此代码在较旧的计算机或大型文件上运行,则这种方法会占用大量内存,因此不鼓励使用。@jrockway-如果您仅使用$line=Perl循环一个文件,则会添加已定义的检查,因此OP可能不知道这一点,可能会天真地在$line1=和$line2=时写入,当其中一个读取空行或第0行时,写入操作将失败。