Good day.

I am working on a problem that is just an intellectual challenge. I am not asking for help on solving the actual problem, rather just help on optimizing a loop. As it stands, running the script over the input takes a few hours to complete.

After the initial loading the array @input_connections, will contain pairs of numbers where each element is in a format like '7-12' (starting connection-ending connection). The code snippet below reads through each of these pairs and culls out the ones that are already implied by the other pairs

Whereas if the pair '7-12' was stored and the next pair is '7-10', '7-10' would be rejected because '7-10' falls within the range of '7-12'.

The array @used_connections stores numbers that have been used in each pair. The array @output_connections stores the connections that are to be output from the processing.

Ideally, I'd love to have the connections stored in a hash and be able to check for used numbers with something like ' if exists $hash{key1...keyN} ' thereby removing a loop for checking. I am seeking wisdom to see if there is ( and I am sure there is ) a more efficient way to go about this part.

Any suggestions will be most appreciated. Thanks!

I apologize if this does not make sense.

Code snippet

############################################################### my $pair_no = 1; foreach my $conn (@input_connections) { print "\rON pair $pair_no"; my @curr_conn = split(/\-/,$conn); ## check if current connection exists my $exists = 1; my $start = $curr_conn[0]; my $end = $curr_conn[1]; ## first number in pair is larger than second, reverse them if ($curr_conn[0] > $curr_conn[1]) { $start = $curr_conn[1]; $end = $curr_conn[0]; } ## ** Ideally I want to do away with this loop ** ## for ($start..$end) { if ( not defined $used_connections[$_] ) { $exists = 0; last; } } if ($exists == 0) { for ($start..$end) { $used_connections[$_] = 1; # I tried something like @used_connections[$start..$end] = + 1 , but # it did not seem to work the same as what is in place now +. } push @output_connections , $conn; } $pair_no++; } ###############################################################

In reply to Request help on optimizing a loop for looking up numbers by VincentK

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.