找到规律后如何读线?

问题描述 投票:0回答:1

我有台词

CHANNEL(SYSTEM.DEF.SVRCONN) CHLTYPE(SVRCONN)
id=3

我想做的是在搜索文件并找到第一行后检索 id。

open(CHECKFILE8, "$file");
while (<CHECKFILE8>) {             #while loop to loop through each line in the file
    chomp;                         #take out each line by line
    $datavalue = $_;               #store into var , $datavalue.
    $datavalue =~ s/^\s+//;        #Remove multiple spaces and change to 1 space from the front
    $datavalue =~ s/\s+$//;        #Remove multiple spaces and change to 1 space from the back
    $datavalue =~ s/[ \t]+/ /g;    #remove multiple "tabs" and replace with 1 space
    if ($datavalue eq "CHANNEL(SYSTEM.DEF.SVRCONN) CHLTYPE(SVRCONN)") {
        # HOW TO READ THE NEXT LINE?
    }
}
perl
1个回答
1
投票

与您阅读所有其他行的方式相同:使用表达式

<CHECKFILE8>
。例如:

my $nextline = <CHECKFILE8>;

不过,您应该知道,通过裸字标识符打开文件句柄有点古老的 Perl。让 Perl 在词法范围的变量中为您创建文件句柄通常更安全、更惯用:

open my $checkfile8, '<', $file or die "Can't open $file: $!";
while (<$checkfile8>) { ... }

您正在使用

use strict
,对吗?

© www.soinside.com 2019 - 2024. All rights reserved.