Perl 读取行问题
我要读取一个文件,例如 test.test
其中包含
#test:testdescription\n
#cmd:binary\n
#return:0\n
#stdin:|\n
echo"toto"\n
echo"tata"\n
#stdout:|\n
toto\n
tata\n
#stderr:\n
我成功获取的 #test: 之后的内容; #cmd:等等... 但对于 stdin
或 stdout
,我想将下一个 #
之前的所有行放入表 @stdin
和@stdout。
我做了一个循环 while ($line =
所以它会查看每一行。如果我看到一个模式 /^#stdin:|/
,我想移动到下一行并将该值设置为 表直到我看到下一个#
。
如何移至 while
循环中的下一行?
I'd to read a file, e.g. test.test
which contains
#test:testdescription\n
#cmd:binary\n
#return:0\n
#stdin:|\n
echo"toto"\n
echo"tata"\n
#stdout:|\n
toto\n
tata\n
#stderr:\n
I succeeded in taking which are after #test: ; #cmd: etc...
but for stdin
or stdout
, I want to take all the line before the next #
to a table @stdin
and @stdout
.
I do a loop while ($line = <TEST>)
so it will look at each line. If i see a pattern /^#stdin:|/
, I want to move to the next line and take this value to a
table until i see the next #
.
How do I move to the next line in the while
loop?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过为
$ 选择适当的值,可以轻松处理此文件格式。 /
:输出:
This file format can be easily handled with some creativity in selecting the appropriate value for
$/
:Output:
根据用户的评论进行更新
如果我正确理解了问题,您想在循环中再阅读一行吗?
如果是这样,您可以:
只需在循环内读取另一行。
保留一些状态标志并在循环的下一次迭代中使用它,并在缓冲区中的标准输入之间累积行:
此解决方案可能无法 100% 满足您的需求,但它定义了您在状态机实现中需要遵循的模式:读取一行。检查您当前的状态(如果重要的话)。根据当前状态和行中的模式,验证对当前行执行的操作(添加到缓冲区?更改状态?如果更改状态,则根据上一个状态处理缓冲区?)
另外,根据您的评论,你的正则表达式中有一个错误 - 管道(
|
字符)在正则表达式中意味着“OR”,所以你说“如果行以#stdin
开头或匹配空正则表达式” - 后一部分始终为真,因此您的正则表达式将 100% 匹配。您需要转义“|”通过/^#stdin:\|/
或/^#stdin:[|]/
UPDATED as per user's colmments
If I understand the question correctly, you want to read one more line within a loop?
If so, you can either:
just do another line read inside the loop.
Keep some state flag and use it next iteration of the loop, and accumulate lines between stdins in a buffer:
This solution may not do 100% of what you need but it defines a pattern you need to follow in your state machine implementation: read a line. Check your current state (if it matters). Based on the current state and a pattern in the line, verify what do do about the current line (add to the buffer? change the state? If changing a state, process the buffer based on last state?)
Also, as per your comment, you have a bug in your regex - the pipe (
|
character) means "OR" in regex, so you are saying "if line starts with#stdin
OR matches an empty regex" - the latter part is always true so your regex will match 100% of time. You need to escape the "|" via/^#stdin:\|/
or/^#stdin:[|]/