in reply to Perl code help
Update: I personally like Athanasius's code. But here is another way to do it without the flip/flop operator... Here the "state" of inside record or not is handled by being inside the process_record subroutine or not. At some point, you may find this parsing technique of use...I added some junk to the data and moved the START line from the beginning. If you want the junk to be preserved, maybe these are comments? modify the first while loop. Here I deleted ignored it.
#!/usr/bin/perl use strict; use warnings; while (my $line = <DATA>) { process_record($line) if $line =~ /START/; } sub process_record { my ($line) = @_; my @lines; my $found; push @lines, $line; #the "START" line while ( $line = <DATA>, $line !~ /END/) { $found = 1 if $line =~ /def/; push @lines, $line; } push @lines, $line; #the "END" line print @lines unless $found; return; } =prints START xyz abc END =cut __DATA__ START abc def ghi END junk more junk START xyz abc END
|
|---|