如何从perl中的文件中读取两行

时间:2016-10-11 07:02:38

标签: perl

我想知道为什么我似乎无法在Perl中使用2行 我的代码片段是

#!/usr/bin/perl -w

open my $fh, "<", "data.txt" or die;

$line1 = <$fh>;
$line2 = <$fh>;

print $line1."\n";
print $line2."\n";

while(1){
    @rows = <$fh>;
    print "$rows[$e]";
    $e++;
    print "$rows[$e]";
}

这是我在Data.txt中的数据

  5000    5000    12497500    0
  5000    5000    33258530    0
  5000    13    51822    0
  5000    13    130147    0
  5000    28    75016    0
  5000    28    181343    0
  5000    5000    4999    4999
  5000    5000    12580635    4999

我已经四处寻找可能的解决方案,我在这里发现了一些有趣的线索http://www.perlmonks.org/?node_id=620077,尤其是这些

my $fh = open("file");

for =$fh->$line1,$line2 {
  # do whatever with the two lines
}

for (;;) {
   my $line1 = <$fh>;
   last if not defined $line1;

   my $line2 = <$fh>;
   last if not defined $line2;

   ...
}

我还能问一下for =$fh->$line1,$line2为什么有一个= $,为什么它不起作用或者它是如何工作的?

感谢任何帮助!

干杯

更新 好吧,所以我可能会遗漏某些东西,但这是我想从行中做的操作

while(1){
    print '-'x50 . "\n";
    my $line1 = <$fh>;

    if($line1 =~ /^\s+(\d)\s+(\d)\s+(\d)\s+(\d)\s*$/){
        my $A1 = $1;
        my $B1 = $2;
        my $C1 = $3;
        my $D1 = $4;
    }
    last unless defined $line1;

    my $line2 = <$fh>;

    if($line2 =~ /^\s+(\d)\s+(\d)\s+(\d)\s+(\d)\s*$/){
        my $A2 = $1;
        my $B2 = $2;
        my $C2 = $3;
        my $D2 = $4;
    }
    last unless defined $line2;

    my @type = ("AL", "PL", "RL", "AA", "PA", "RA", "AB", "PB", "RB", "AR", "PR", "RR", "AS", "PS", "RS", "AV", "PV", "RV");


    print("For $type[$i]\n");
    print("Node : $A1\n");
    print("Depth : $B1\n");
    print("Nb : $C1\n");
    print("Nb+s : $C1+$C2\n");
    print("Nk : $A1\n");
    print("Nu : $A1/6\n");
    print("Average number of comparisons is $C2/(7/6*$A1)\n");
    $i++;
}
print '--- END ---'."\n";

这是基于xxFelixxxx的回复

2 个答案:

答案 0 :(得分:3)

while循环中,您调用@rows = <$fh>;,它会占用您文件的所有行并将其放入@rows。下一次通过while循环,它会尝试做同样的事情......但是没有剩下的行,所以@rows将为空。因此,如果您需要所有行,请在while循环之外抓取它们。如果你一次只需要2行,你可以在循环中抓取2行并检查它们是否为空:

#!/usr/bin/env perl

use strict;
use warnings;

open my $fh, "<", "data.txt" or die "Unable to open data.txt for reading : $!";

while(1) {
    print '-'x50 . "\n";
    my $line1 = <$fh>;
    last unless defined $line1;
    print "ODD  LINE $. : $line1";

    my $line2 = <$fh>;
    last unless defined $line2;
    print "EVEN LINE $. : $line2";
}
print '--- SUCCESS ---' . "\n";

<强>输出

--------------------------------------------------
ODD  LINE 1 :   5000    5000    12497500    0
EVEN LINE 2 :   5000    5000    33258530    0
--------------------------------------------------
ODD  LINE 3 :   5000    13    51822    0
EVEN LINE 4 :   5000    13    130147    0
--------------------------------------------------
ODD  LINE 5 :   5000    28    75016    0
EVEN LINE 6 :   5000    28    181343    0
--------------------------------------------------
ODD  LINE 7 :   5000    5000    4999    4999
EVEN LINE 8 :   5000    5000    12580635    4999
--------------------------------------------------
--- SUCCESS ---

<强>更新

在您的更新版本中,您有一个范围问题,即

的构造
if (...) { 
    my ($foo) = 123;
}
print $foo; # <--- out of scope

表示代码的其余部分不会看到变量$A1,$B1,$C1,$D1。正则表达式的使用不是必需的,可以使用空格上的split和数字上的grep更清楚地表达,以进行健全性检查。 my @foo = ("abc","def","ghi");的结构更加完美地表达为my @foo = qw( abc def ghi );

以下是更新版本:

#!/usr/bin/env perl

use strict;
use warnings;

open my $fh, "<", "data.txt" or die "Unable to open data.txt for reading : $!";

my @types = qw(AL PL RL AA PA RA AB PB RB AR PR RR AS PS RS AV PV RV);

while(1) {
    print '-'x50 . "\n";
    # TODO: better names than a,b,c,d
    my ($A1,$B1,$C1,$D1) = get_row($fh);
    last unless defined $A1;

    my ($A2,$B2,$C2,$D2) = get_row($fh);
    last unless defined $A2;

    my $type = shift @types; # Pull one off the front
    defined $type
        or die "No more types left!";

    my $Nb_plus_s = $C1 + $C2;
    # sprintf("%0.2f", 123.456789) -> 123.46   # round to 2 decimals
    my $Nu = sprintf("%0.2f", $A1 / 6);
    my $avg_comparisons = sprintf("%0.2f", $C2/(7/6*$A1));

    print "For $type\n";
    print "Node  : $A1\n";
    print "Depth : $B1\n";
    print "Nb    : $C1\n";
    print "Nb+s  : $Nb_plus_s\n";
    print "Nk    : $A1\n";
    print "Nu    : $Nu\n";
    print "Average number of comparisons is $avg_comparisons\n";
}
print '--- SUCCESS ---' . "\n";

sub get_row {
    my ($file_handle) = @_;
    my $line = <$file_handle>;
    return unless defined $line;

    # Split on whitespace, keep only numbers
    my @data = grep {/\d/} split /\s+/, $line;

    # Check that we have 4 columns of data
    (4 == scalar @data)
        or die "Cannot understand line: '$line'";

    return(@data);
}

新输出

--------------------------------------------------
For AL
Node  : 5000
Depth : 5000
Nb    : 12497500
Nb+s  : 45756030
Nk    : 5000
Nu    : 833.33
Average number of comparisons is 5701.46
--------------------------------------------------
For PL
Node  : 5000
Depth : 13
Nb    : 51822
Nb+s  : 181969
Nk    : 5000
Nu    : 833.33
Average number of comparisons is 22.31
--------------------------------------------------
For RL
Node  : 5000
Depth : 28
Nb    : 75016
Nb+s  : 256359
Nk    : 5000
Nu    : 833.33
Average number of comparisons is 31.09
--------------------------------------------------
For AA
Node  : 5000
Depth : 5000
Nb    : 4999
Nb+s  : 12585634
Nk    : 5000
Nu    : 833.33
Average number of comparisons is 2156.68
--------------------------------------------------
--- SUCCESS ---

答案 1 :(得分:1)

$line1$line2从文件中读取前两行。问题出在循环中:

@rows = <$fh>;

这会将所有剩余文件读入数组。在列表上下文中,哪个数组赋值,您总是读取所有内容。您可以尝试改为

while (! eof $fh) {
    my $line1 = <$fh>;
    push @rows, "Odd: $line1";
    last if eof $fh;

    my $line2 = <$fh>;
    push @rows, "Even: $line2";
}
print "@rows";

它使用eof来检测输入的结束。