Hello newtoperlprog,

... suggestions in ... reducing the code

Your code reads the input file, extracts the data you need, and stores this to a temporary file. It then constructs a hash by reading that file and extracting its data! So the first improvement is obviously to remove this unnecessary middle step, and create the hash as the input file is read. Here is my re-write of the code between “Make array of rejected length” and “Write HTML file”:

my $input = 'fragments.txt'; my %split; open(my $infile, '<', $input) or die "Cannot open file '$input' for reading: $!"; build_hash($_) while <$infile>; close $infile or die "Cannot close file '$input': $!"; build_hash(length($sequence) + 1); sub build_hash { use feature 'state'; state $old = 0; state $cnt = 1; my ($line) = @_; my $gap = $line - $old; my $start = $old + 1; my $end = ($gap > 2) ? $line - 1 : $start; $split{$cnt++} = [$start .. $end] if $gap >= 2; $old = $line; }

As you can see, the rewritten code is about half as long as the original. Duplicate code has been moved into a subroutine, and variables have been given reduced scope, which will aid in debugging. (Note also that the original code closes $infile twice.)

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: Code optimization help and troubleshooting by Athanasius
in thread Code optimization help and troubleshooting by newtoperlprog

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.