如果您逐行浏览文件,则可以将部分行保留在变量中并将它们与下一行连接起来。通读代码 - 我已经评论了功能。
my $curr;
my $txt;
open ( IN, "<", 'yourinputfile.txt' ) or die 'Could not open file: ' . $!;
while (<IN>) {
chomp;
# if the line ends with a backslash, save the segment in $curr and go on to the next line
if ( m!(.*?) \$! ) {
$curr = $1;
next;
}
# if $curr exists, add this line on to it
if ( $curr ) {
$curr .= $_;
}
# otherwise, set $curr to the line contents
else {
$curr = $_;
}
if ( $curr =~ /set_case_analysis -name\s+\"?(\S+)/) {
# if the string is in quotes, the regex will leave the final " on the string
# remove it
( $txt = $1 ) =~ s/"$//;
print "Set Case $txt\n";
$set_case_analysis{$txt}=1;
}
elsif ($curr =~ /quasi_static -name\s+(\S+)/) {
( $txt = $1 ) =~ s/"$//;
print "Quasi Static $txt\n";
$quasi_static{$txt}=1;
}
elsif ($curr =~ /reset .*?-name\s+\"?(\S+)/) {
( $txt = $1 ) =~ s/"$//;
print "Reset $txt\n";
$reset{$txt}=1;
}
# reset $curr
$curr = '';
}
您可以通过执行以下操作使其更加紧凑和整洁:
if ( $curr =~ /(\w+) -name \"?(\S+)/) {
( $txt = $2 ) =~ s/"$//;
$data{$1}{$txt}=1;
}
您将获得一个嵌套散列结构,其中包含三个键 、set_case_analysis
和quasi_static
,reset
以及来自 的各种不同值-name
。
%data = (
quasi_static => ( foo => 1, bar => 1 ),
reset => ( pip => 1, pap => 1, pop => 1 ),
set_case_analysis => ( foo => 1, bar => 1 )
);