[英]How to print line by line of read c file after removing comments from it using perl
use Regexp::Common qw( comment );
local $/ = undef;
$_ = <DATA>;
s/$RE{comment}{C}//gs;
我已讀取完整的文件數據,並從讀取的文件中刪除了所有注釋。 現在,我要逐行打印,而不是一次打印所有數據。
根據我的理解,完整的文件數據在字符串$_
。 如何將其轉換為數組,以便可以逐行打印。
如果文件數據在數組中,我可以像這樣進行一些字符串匹配
嘗試:
my @filecontents = split /\n/; # assuming the data is in $_
print "$_\n" foreach (@filecontents);
希望能有所幫助
您可以在換行符處分割字符串,然后一一輸出結果列表。
foreach my $line (split /\n/) {
print "$line\n";
}
要么
say for (split /\n/);
如果您的Perl支持say
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.