如何使用 perl 回退一行
谁能告诉我当你迭代文本文件时,如何在 Perl 中返回一行。例如,如果我看到行中的文本并且我认识到它,并且如果它被识别为特定模式,我想回到上一行做一些事情并进一步继续。
提前致谢。
Could anybody tell me how it is possible in perl to go one line back in perl when you iterate over the text file. In instance if I see text in line and I recognize it and if it is recognized as an particular pattern I would like go back to previous line do some stuff and proceed further.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常,您不会返回,只需跟踪上一行:
使用
continue
块可以保证即使您通过next 绕过循环体的一部分,也会进行复制
。如果你想真正倒带,你可以使用
seek
和tell
来完成,但它更麻烦:Normally you don't go back, you just keep track of the previous line:
The use of a
continue
block guarantees that the copy is made even if you bypass part of the loop body vianext
.If you want to truly rewind you can do it with
seek
andtell
but it's more cumbersome: