First of all sugar++ for giving us sample input and reference output as well as a clear description of what your problem is.
In addition to what the others have said: You build a fairly large regex from @dels, which might slow down things (if the number of items is in the millions).
I'd suggest to use a hash instead of @dels:
#!/usr/bin/perl use strict; use warnings; my $prev1=0;my $prev2=0;my @dels; open my $file, '<', 'data.txt' or die "Can't open file: $!@"; my %dels; while (<$file>) { # the \. prevents . from matching any character my @spl = split(/\.[fr]/, $_); if($prev1 eq $spl[0] && $prev2 eq $spl[1]){ $dels{$spl[0]} = 1; } $prev1=$spl[0];$prev2=$spl[1]; } # reset the file cursor, read from the beginning again seek $file, 0, 0; while (<$file>) { my @spl = split(/\.[fr]/, $_); print unless $dels{$spl[0]}; } close $file;
This version gets rid of @arr entirely, and replaced the regex and @dels with %dels.
In reply to Re: to delete a set of specific lines in a file
by moritz
in thread to delete a set of specific lines in a file
by sugar
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |