perl从标量变量逐行读取(perl read line

编程入门 行业动态 更新时间:2024-10-26 20:34:05
perl从标量变量逐行读取(perl read line-by-line from scalar variable)

从抓取网站,我有一个标量变量的html文件,$ res。 我想逐行读取$ res中的html文件。 例如,while(my $ line =)...

我是否需要将$ res打印到文本文件然后读入文本文件?

From scraping a website, I have an html file in a scalar variable, $res. I want to read the html file in $res line-by-line. For example, while (my $line = )...

Do I need to print $res to a text file and then read in the text file ?

最满意答案

要解决此问题的Y部分 ,是的,您可以将标量变量视为输入源并使用Perl的输入处理功能。 您只需open对变量的引用:

open my $fh, '<', \$res; my $header = <$fh>; # first "line" of $res while (my $line = <$fh>) { # next "line" of $res ... }

To address the Y part of this problem, yes, you can treat a scalar variable as an input source and use Perl's input processing features. You just open a reference to the variable:

open my $fh, '<', \$res; my $header = <$fh>; # first "line" of $res while (my $line = <$fh>) { # next "line" of $res ... }

更多推荐

本文发布于:2023-08-04 06:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1409442.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:标量   变量   perl   read   line

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!