我很奇怪,爲什麼我似乎無法採取2號線在Perl 我的代碼片段是如何讀取在Perl從文件中兩行
#!/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]";
}
這是一種數據I在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
爲什麼會有= $爲什麼它不工作或者它是如何工作的?
任何幫助表示讚賞!
乾杯
UPDATE 好了,所以我可能會丟失的東西,但是這是我想從線
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的迴應
自從我寫了perl之後過去了一段時間,但'while(1){'看起來像一個無休止的無限循環。我假設這是故意的,但以防萬一...擡起頭來。另外,$ e初始化在哪裏? –
'for = $ fh - > $ line1,$ line2'正在使用'perl6',這應該被認爲是不同的,但相關的語言是'perl'又名'perl5'。 – xxfelixxx
將'strict strict;'添加到所有腳本中,並修復它所抱怨的所有問題,這將導致更少的錯誤代碼。 – xxfelixxx