Upon looking at this code further, I have a few suggestions.

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.


In reply to RE: Help required to improve code by Petruchio
in thread Help required to improve code by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.