As jcwren said, putting the ports you're looking for in an array, and then looking for them all sequentially is a good start:
my @bad_ports = qw(12345 12346 20034 8787 31337 31338 54320 54321); while(<FWLOG>){ foreach my $port (@bad_ports) { if (/\b$port\b/) { print; last; } } }
The problem with this code's performance is, each time through the foreach() loop, the regular expression is recompiled. One step better would be to build an alteration out of the array:
# I'd separate this out and put it in a configuration section of th +e script my $bad_ports = '12345|12346|20034|8787|31337|31338|54320|54321'; my $found = 0; while(<FWLOG>){ print if (/\b($bad_ports)\b/o); }
The /o on the regular expression means "even though there's a variable in there, it wont' change, so compile the regex only once."

In the spirit of "there's more than one way to do it," the last option I'll present is to use a hash. In this method, you do one regular expression match on your logfile line to extract the port (I don't know what your log lines look like, so you'll need to do that), and then you do a hash lookup to see if it's a bad port.

# Build the hash my %bad_ports = (12345=>1, 12346=>1, 20034=>1, 8787=>1, 31337=>1); # s +hort list while (<FWLOG>) { my $port = extract_port_from($_); # fill in a regex here print if $bad_ports{$port}; }
I used a hash here, although in this case an array would work just as well since there are only numerical indices. I believe using an array would allocate space for 0..54321 however, which is not ideal.

I hope these techniques are useful to you! And a small warning: while I made my best effort to write working code, I didn't test it.

Alan


In reply to RE: I want to be a Perl Jedi by ferrency
in thread I want to be a Perl Jedi by dru145

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.