简体   繁体   中英

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;

I have read complete file data and removed all comments from the read file. Now I want to print line by line instead of printing all data at once.

as per my understanding complete file data is in string $_ . How to convert it into a array so that I can print it line by line.

If the file data is in array I can do some string matching like that

Try:

my @filecontents = split /\n/; # assuming the data is in $_
print "$_\n" foreach (@filecontents);

Hope that helps

You can split your string at newline, and output the result list one by one.

foreach my $line (split /\n/) {
    print "$line\n";
}

Or

say for (split /\n/);

If your Perl supports say .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM