You can build your regex on the fly:
my @bad = qw(fork smurf);
my $regex = join('|', @bad);
$regex = qr/$regex/;
foreach (<FH>){
if(/^[0000-9999](.+)/){
next if /$regex/;
push( @witty_quotes, substr( $_, 5) );
} else { next };
}
Some other notes:
<list>
Your quote is already stored in $1 after the first pattern match, so you don't need the substr().
The else { next }; after the if() is redundant.
/^[0000-9999](.+)/ doesn't do what you think it does. :) [0000-9999] is the same as [0-9].
</list>
You can rewrite your code like so:
use strict;
my @witty_quotes;
my @bad = qw(fork smurf);
my $regex = join('|', @bad);
$regex = qr/$regex/;
# open() FH somewhere
foreach (<FH>){
# Note: it's important to put this regex test
# outside of the if() block to ensure that $1
# below comes from the correct pattern match
next if /$regex/;
if(/^\d{4}(.+)/){
push( @witty_quotes, $1);
}
}
-Matt
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.