Perl:使用触发器功能并从读取的块内提取数据
我有一个名为 @mytitles
的数组,其中包含很多标题,例如 title1
、title2
等。我有一个名为“Superdataset
”的文件,其中包含与每个标题相关的信息。然而,与 title1
相关的信息可能有 6 行,而与 title2
相关的信息可能有 30 行(随机)。每条信息(对于 titlex
)均以“Reading titlex
”开头,并以“Done read titlex
”结尾。
我需要从每个标题的这些信息行中提取一些数据。我认为幸运的是,我需要的数据每次都在“Done read titlex
”之前的两行
所以我的“Superdataset
”看起来像:
Reading title1 random info line1 random info line2 random info line3 random info line4 random info line5 my earnings are 6000 my expenses are 1000 Done reading title1 Reading title2 random info line6 random info line7 random info line8 random info line9 random info line10 random info line11 random info line12 random info line13 random info line14 my earnings are 11000 my expenses are 9000 Done reading title2
我需要一个总和费用和收入总额。有什么建议吗? PS-数组的名称很复杂,而不是像 titlex
这样简单
I have an array called @mytitles
which contains a lot of titles such as, say, title1
, title2
and so on. I have a file called "Superdataset
" which has information pertaining to each title. However, the info related to title1
may be of 6 lines while the info for title2
may be 30 lines (its random). Each piece of information (for a titlex
) starts with "Reading titlex
" and ends with "Done reading titlex
".
From these lines of information of each title, I need to extract some data. I think its lucky that this data I need is in the 2 lines just before "Done reading titlex
" each time
So my "Superdataset
" looks like:
Reading title1 random info line1 random info line2 random info line3 random info line4 random info line5 my earnings are 6000 my expenses are 1000 Done reading title1 Reading title2 random info line6 random info line7 random info line8 random info line9 random info line10 random info line11 random info line12 random info line13 random info line14 my earnings are 11000 my expenses are 9000 Done reading title2
I need a total sum of expenses and a total sum of earnings. Any suggestions?
PS-the array has complicated names, not something as simple as titlex
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是将数据转化为可用形式的第一步。
使用创建的数据结构来确定总收入和支出。
以对计算机更有用的形式打印出来。
Here is a first pass at slurping the data into a usable form.
Using the created data structure to determine the total earnings, and expenses.
To instead print it out in a form more useful to a computer.
使用“范围”运算符,您可以执行以下操作:
...这会产生:
Using the 'range' operator you could do:
...which yields:
除非您可以预测相关行之前的行是什么,否则触发器运算符不会通过优化发挥多大作用。我认为使用缓冲区数组会更容易,并且只需匹配收入和支出之后的行即可。
输出:
Unless you can predict what the line before the relevant lines are, the flip-flop operator won't do much good by way of optimization. I think it would be easier to work with a buffer array and just match for the line after the earnings and expenses.
Output: