in reply to Help required to improve code
First, you must tidy it up. Really. It'll save you pain. The reason I was able to see how to get rid of the
while(@results = $sth -> fetchrow_array)
loop is because it's by far the neatest part of your code. Make your variable names shorter (especially ones which don't travel far, like a loop counter), and make your indentation very regular so that this:
$SearchResults{'keyterm'} = $dict{'Searchphrase'} || $dict{'Keyterm'}; + $SearchResults{'fieldname'} = $dict{'Fieldname'}; $SearchResults{'lastcount'} = $dict{'Lastcount'} || 0; $SearchResults{'pageno'} = $dict{'Page'} || 0;
looks like this:
$srchRes{'keyterm'} = $dict{'Searchphrase'} || $dict{'Keyterm'}; $srchRes{'fieldname'} = $dict{'Fieldname'}; $srchRes{'lastcount'} = $dict{'Lastcount'} || 0; $srchRes{'pageno'} = $dict{'Page'} || 0;
Believe me, I don't say this because I'm any kind of neat freak. It will make your life much, much easier.
Once you've done this, look at your loops. If you're saying something like:
if ($a){ $b; $c; } else{ $b; $d; }
You should rewrite it to say:
$b; if ($a){ $c; } else{ $d; }
This happens more than once in your code...
if ($SearchResults[$index]{'startpage'} == $SearchResults[$index]{'end +page'}) { $printHypertextLink .= qq(page(s): $SearchResults[$ind +ex]{'startpage'}</a></font></h3><br>); } else { $printHypertextLink .= qq(page(s): $SearchResults[$ind +ex]{'startpage'}-); $printHypertextLink .= qq($SearchResults[$index]{'endp +age'}</a></font></h3><br>);
should look more like:
$printHL .= qq/page(s): $srchRes[$i]{'startpage'}/; unless ($srchRes[$i]{'startpage'} == $srchRes[$i]{'endpage'}){ $printHL .= qq(-$srchRes[$i]{'endpage'}); } $printHL .= qq(</a></font></h3><br>);
Same goes for your loops... you don't need to build the same literal string every time you iterate through a for loop.
And then... there are your variables. All of your variables are global. If your script is worth optimizing... if it's worth keeping... do something about this. Certain parts of your script are NOT doing what you think they are. Besides, it's very difficult to change anything without breaking the whole script, when any change you make will affect the whole script. Study up on the my keyword. Your sanity may depend on it.
|
|---|