in reply to Code optimization help and troubleshooting

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,