Here's a first pass at cleaning up the main loop of your code. It's not tested, so don't trust it, but it ought to do exactly the same thing, faster.
The code is easier to read this way. So easy that I can see a BUG! (I left it in with a comment).
The program could be made even clearer and further optimized, but this is a start. For example you can replace all the print DAT $c; with $all .= $c; and, wait till the end to open the file for append, print DAT $all;, and close.
By the way, if you can, you should test this in a standalone program on your computer, not just on the web.
# 1. Only open/close the file once to append, instead of $maxid times.
# 2. Use temporary values.
# 3. Delete stuff you don't need immediately, not next time around loo
+p.
# 4. In this case, ($c) is same as (defined $c) (cosmetic).
open(DAT,">>data/parsed-all.txt");
for(my $i = 1; $i <= $maxid; $i++) {
my $currid = changeID($i);
my ($c1,$c2,$c3) = ($compare1{$currid}, $compare2{$currid}, $compare
+3{$currid});
delete $compare1{$currid}, $compare2{$currid}, $compare3{$currid};
next if( $c1 && $c2 && $c3 && $c1 ne $c2 && $c1 ne $c3 && $c2 ne $c3
+ );
if( $c1 && !$c2 && !$c2 ) {
print DAT $c1;
next;
}
if( $c2 && !$c1 && !$c3 ) {
print DAT $c2;
next;
}
if( $c3 && !$c1 && !$c2 ) {
print DAT $c2; # <-- BUG HERE!
next;
}
if( $c1 && $c2 ) {
if( $c1 eq $c2 ) {
print DAT $c1;
next;
}
}
if( $c1 && $c3 ) {
if( $c1 eq $c3 ) {
print DAT $c1;
next;
}
}
if( $c2 && $c3 ) {
if( $c2 eq $c3 ) {
print DAT $c2;
next;
}
}
}
close(DAT);
-
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.