digger has asked for the wisdom of the Perl Monks concerning the following question:

Esteemed Monks,

I am working on another script that makes changes to postscript files. This time I am trying to delete lines that ask for paper types that aren't going to be used in the document. My sub takes the current line and a list of media types to remove.

Here's my sub and the sample data I copied out of a larger file for testing.
sub kill_media { my $line = shift; my $media; if ($line =~ m!^%%DocumentMedia:!){ $media_killed=1; print OUT $line; while ($line = <IN>){ foreach $media (@_) { if ($line =~ m!$media!){ $line = ""; print "Killed media type: $media\n"; } elsif ($line !~ m!^%%+!){ return $line; } } print OUT $line; } } ----SAMPLE DATA----- %XRXbegin: 001.014 %XRXtitle: JOB P04063001.X0026 - Wps %XRXsenderName: CompuSet %XRXdisposition: PRINT %XRXcopyCount: 1 %XRXrequirements: duplex %XRXsignature: False %XRXorientation: Portrait %XRXxImageShift: 0 %XRXyImageShift: 0 %XRXend %!PS-Adobe-3.0 %%DocumentMedia: anything 612 792 0 white () %%+ anything 612 792 0 blue () %%+ anything 612 792 0 green () %%+ anything 612 792 0 pinky () %%+ anything 612 792 0 UNIVERSALID () %%Creator: CompuSet Version 8.0.0 %%CreationDate: 3/03/2004 16:04:28 %KDKHost: Binder %%BoundingBox: 0 0 612 792 %%Pages: 182


I want to kill the pinky and UNIVERSALID media calls. The strange thing is the line for UNIVERSALID doesn't get deleted when it is the last one. If I swap the pinky and UNIVERSALID lines, both get deleted correctly. This isn't making any sense to me, so I am seeking the wisdom of the monks.

digger

Replies are listed 'Best First'.
Re: Simple regex gone wierd
by Roy Johnson (Monsignor) on Apr 19, 2004 at 15:08 UTC
    In your foreach loop, on a successful match, you empty line. The next pass through the foreach loop, you're going to fail to match ^%%+ and thus return.

    The fix is to put a last after the print "Killed media type: $media\n". Once you've killed, after all, you needn't bother with more checking.

    Alternatively, and probably better, is to put the check for ^%%+ before the foreach loop.


    The PerlMonk tr/// Advocate