Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

RE: I want to be a Perl Jedi

by nuance (Hermit)
on Jul 18, 2000 at 17:41 UTC ( #22996=note: print w/replies, xml ) Need Help??


in reply to I want to be a Perl Jedi

Three suggestions spring to mind for combining the print statements, you could either combine the tests into one regular expression:
print "$_" if /\b12345\b| \b12346\b| \b20034\b| \b8787\b| \b31337\b| \b31338\b| \b54320\b| \b54321\b/x;
or combine the different regular expressions into one logical test:
print "$_" if (/\b12345\b/ or /\b12346\b/ or /\b20034\b/ or /\b8787\b/ or /\b31337\b/ or /\b31338\b/ or /\b54320\b/ or /\b54321\b/);
or use a little loop:
my @ports = map { qr/$_/ } ("\b12345\b", "\b12346\b", "\b20034\b", "\b8787\b", "\b31337\b", "\b31338\b", "\b54320\b", "\b54321\b"); while (<FWLOG>) { for my $port (@ports) { if ($_ =~ $port) { print; last; } } }
(This idea for the last one was pinched from jcwren, implementation pinched from various others).

Nuance

Replies are listed 'Best First'.
RE: RE: I want to be a Perl Jedi
by jlistf (Monk) on Jul 18, 2000 at 17:49 UTC
    if you're going to start combining regular expressions, you may as well condense them further.

    /\b12345\b/ and /\b12346\b/
    can become
    /\b1234[56]\b/

    and
    /\b31337\b/ and /\b31338\b/
    can become
    /\b3133[78]\b/

    and
    /\b54320\b/ and /\b54321\b/
    become
    /\b5432[01]\b/
    hope this helps some more.

    Update (to jcwren): good point. i totally agree. i didn't realize this was port numbers and that they might change in the future.
      jlistf, I can't (and won't) disagree that the regexp optimizations you've offered do optimize the search. But this is really the kind of data that should not be optimized. Since ports can change, new scanners can come (and go), etc, optimizing the regexp seriously decreases the maintainability of the script. The next person that comes along may not be a Perl programmer.

      To this end, the script should contain an array at the top, with the list of port numbers, along with explicit instructions as to how to add/delete/change a port number. The next person shouldn't have to learn regexp just to update the ports to be checked for.

      So, you get a virtual ++ for reducing the regexp, but a virtual -- for making it more unmaintainable. (which really means I just don't vote on the node).

      --Chris

      e-mail jcwren

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://22996]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2022-05-22 14:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (80 votes). Check out past polls.

    Notices?