可能重复:
How do I break out of a loop in Perl?的Perl - 停止读取一个文件,如果多行匹配
我有数据,看起来就像你所看到的波纹管。我正在尝试创建一个将捕获选定文本的perl脚本。我的想法是说“如果上一行读取全是 - 并且当前行读取全部=',那么请停止阅读文件,并且不要仅用=和 - 打印这些行
但是,我不知道如何编写代码,我只是在3天前开始使用perl,我不知道这是否是最好的方式,让我知道是否有更好的方法如果 无论哪种方式,你可以用代码的帮助,我会很感激的
到目前为止我的代码:
...
$end_section_flag = "true" # I was going to use this to signify
# when I want to stop reading
# ie. when I reached the end of the
# data I want to capture
while (<$in-fh>)
{
my $line = $_;
chomp $line;
if ($line eq $string)
{
print "Found it\n";
$end_section_flag = "false";
}
if ($end_section_flag eq "false")
{
print $out-fh "$line\n";
// if you found the end of the section i'm reading
// don't pring the -'s and ='s and exit
}
}
什么我的数据看起来像
-------------------------------------------------------------------------------
===============================================================================
BLAH BLAH
===============================================================================
asdfsad
fasd
fas
df
asdf
a
\n
\n
-------------------------------------------------------------------------------
===============================================================================
BLAH BLAH
===============================================================================
...
我想捕捉,因为你的边界跨越行尾
-------------------------------------------------------------------------------
===============================================================================
BLAH BLAH
===============================================================================
asdfsad
fasd
fas
df
asdf
a
\n
\n
您没有以粗体显示任何文本。 – Borodin 2012-07-12 14:58:05
[我如何摆脱Perl中的循环?](http://stackoverflow.com/questions/303216/how-do-i-break-out-of-a-loop-in-perl) – 2012-07-12 14:58:31
真实世界例子应该有所帮助从你的问题不清楚你想要达到什么。显示:1)你有什么2)和你想得到什么3)变成什么变量。你的代码没有关于内容的任何条件 - “$ end_section_flag”还没有任何说明。 – jm666 2012-07-12 14:58:45