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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|