使用perl,如果在每行的情况下选择最后2行有相同的单词?

时间:2013-09-03 16:14:28

标签: arrays perl

Bini  --  -21.89753  -20.47853  -20.27835  -18.34952  -16.23454

Bini  --  -16.89753  -14.47853  -13.27835  -12.34952  -11.23454

Bini  --  -10.09014  

我的文件有一个如上所示的数组。 而这个以Bini开头的数组是具有多行的数组,但我在这里只显示了3行。 我想尝试的是从最后两行中提取最后3个元素。 所以,-12.34952 -11.23454 -10.09014这3个元素是我想要的。 有时,最后一行可能有2到5的元素,具体取决于文件。但是在这里,它只有最后一行的1个元素。

我尝试的内容如下

while(my $line = <FILE>) {
     if($line =~ /Bini/) {      #extract last 3, 2, 1 element
     my @entries = split(/Ws+/,$line);
     $element1 = (pop@entries);
     $element2 = (pop@entries);
     $element3 = (pop@entries);
     }

结果,我可以看到element1是-10.09014,但遗憾的是,我无法获得元素2和元素3。 有人能帮助我吗? ..


我想保留原始脚本。我的意思是,创建result.txt的过程和输出格式的“log”的打开方法。

  

块引用

#!/usr/bin/perl use warnings; 
use strict; 
use File::stat; 

open (OUT, ">", "result\.txt") or die "cannot open file\,\n"; #from this plx, I want to creat result.txt 

foreach my $answer (glob "*.log") { # format of reading file will be "log" 

open (FILE, "<", "$answer") or die "Cannot open file\.\n"; 
my @file = split ('\.', $answer);
  

块引用

文件导致错误的打开方法(我的@array = read_file('input.txt') 我想知道如何从脚本中的$ line = 0开始使用脚本。 即使我将txt的格式更改为log(例如input.log),它仍然给出了错误消息。 (read_file'input.txt' - sysopen:text.plx第6行没有这样的文件或目录)

.....

2 个答案:

答案 0 :(得分:2)

您可以从每一行获取所有数字,在@entries的末尾推送它们,并且始终只保留最后三行。

my @entries;
while(my $line = <FILE>) {
     next if $line !~ /Bini/;
     push @entries, grep /\d/, split /\s+/,$line;
     @entries = @entries[-3 .. -1] if @entries > 3;
}
print join "\n", @entries;

输出

-12.34952
-11.23454
-10.09014

答案 1 :(得分:0)

•••注意••••此处的输入文件特定于您最近被阻止的问题(using perl, how to select last 2 lines in the case of each row has same word?(re-questioned..)),因此仅适用于该输入。

这是您最近提供的输入:

Bini  --  -10.09014  

cidi

Bini  --  -21.89753  -21.47853  -20.27835  -20.34952  -17.23454

Bini  --  -16.89753  -12.47853  -11.27835  -11.34952  -10.23454

Bini  --  -09.09014  

light is 3.4

这将完全达到你想要的效果(即从'Bini'开始从倒数第二行中提取最后两个元素,以'Bini'结尾的最后一行中的最后一个元素提取),但不再...

#!/usr/bin/perl
use warnings;
use strict;

my $file = 'location/of/your/file.txt';
open my $input, '<', $file or die "Can't write to $file: $!";

my $line = 0;
my (@element1, @element2, @element3);
while (<$input>){ 
    chomp;
    next if ($_ =~ /^\s*$/); # skips a line of input if the line is blank
        if ($_ =~ /^Bini/) { # if the line starts with Bini
            $line++; # Add 1 to count variable
            my @split = split('\s+'); # split by spaces
            if ($line == 3) { # if the line number = 3 (i.e. second to last)
                push @element1, $split[-1]; # add the last element of split (-10.234...) to @element1
                push @element2, $split[-2]; # # add the second-to-last element of split to @element2
            }
            elsif ($line == 4) { # if the line number is 4 (last line starting with Bini
                push @element3, $split[-1]; # # add the last element of split to @element1
            }
        }
}

print "$element3[0]\t$element1[0]\t$element2[0]\n";

输出:

-09.09014   -10.23454   -11.34952