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 ... }更多推荐
发布评论