in reply to I want to be a Perl Jedi

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

Replies are listed 'Best First'.
RE: RE: I want to be a Perl Jedi
by athomason (Curate) on Jul 18, 2000 at 21:59 UTC
    Just because TIMTOWTDI, I put together a version using the qr// operator. I had guessed it would be more efficient than the /o version, but such is not the case:
    use Benchmark; push @lines, ((' ' x int rand 12) . (int rand 60000) . (' ' x int rand + 2)) for 1..10000; @pattern_list = qw(12345 12346 20034 8787 31337 31338 54320 54321); my $bad_ports = '\b(?:12345|12346|20034|8787|31337|31338|54320|54321)\ +b'; timethese( 100, { 'qr' => 'with_qr', '/o' => 'with_o' }); sub with_qr { foreach $pattern (@pattern_list) { my $re = qr/\b${pattern}\b/; foreach $line (@lines) { $line =~ /$re/; } } } sub with_o { my $found = 0; for(@lines){ (/\b(?:$bad_ports)\b/o); } }
    with these results:
    Benchmark: timing 100 iterations of /o, qr... /o: 7 wallclock secs ( 6.88 usr + 0.00 sys = 6.88 CPU) @ 14 +.53/s (n=100) qr: 20 wallclock secs (18.84 usr + 0.00 sys = 18.84 CPU) @ 5 +.31/s (n=100)

    Granted, this is a poor dataset, and mileage will probably vary for a real logfile.