Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Hello Monks,

I have two arrays of numbers like so:
my @range = (50000 .. 65535); my @ports = (50000..51545,51549,51555..51999,@more);

where the @ports array was populated from a spreadsheet and is subject to change (and the range may not be permanently fixed either). The @ports array will always be a subset of @range, though and @range will never have gaps in numbering. I'm trying to find the first open port in @ports for a given range and figure there has to be a more efficient way. What I have is a glorified increment by 1 and compare operation. I'm using 5.8 on a work server and can't upgrade to get the smart match operator.
Update: By open port, I mean port that isn't being used and therefore isn't in @ports.
my @hash{@ports} = (1) x @ports; #set indices to true for ports in use my $open_port; foreach my $port (@range){ $open_port = $port and last unless $hash{$port}; }

I've read some of the posts and a FAQ How can I tell whether a certain element is contained in a list or array?, but none seem to quite do what I want, i.e. look for what isn't there in the most efficient manner as they all seem to do an increment by one or loop one by one, too. I thought of maybe removing indicies of a hash keyed from @range by another set of indicies from @ports instead of looping but my brain hurts now.


In reply to fastest way to compare two arrays by krusty

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (3)
    As of 2017-12-17 03:15 GMT
    Find Nodes?
      Voting Booth?
      What programming language do you hate the most?

      Results (462 votes). Check out past polls.