in reply to searching for keywords
The syntax error is because you don't have a block for your 'if' statement, only for the 'while'. You could get by without the block if you turned it into a modifier (see perlsyn), as follows:
print $_ if( $_ =~ m/$keyword/ );
There's more to it than that, though, because you're using the assignment operator ('=', see perlop) in what appears to be a pattern match, hence the modification I made to your code in the above example. If @keywords is large, use Super Search to find examples of using an array in a pattern match. If it is reasonably small, you can use alternation (perlre):
my $pattern = join( '|', @keywords ); if( $_ =~ m/$pattern/ )
Oh, and in addition to using strict and warnings, you may also find diagnostics helpful.
HTH
ikegami is absolutely right about escaping special characters, of course (see quotemeta). Thanks and ++ for pointing that out!
Update: full code example, with escaped characters
use strict; use warnings; my @keywords = qw( keyword1 keyword2 keyword3 ); my $pattern = join( '|', map quotemeta, @keywords ); print "pattern = [$pattern]\n"; # using 'if' as a modifier while( my $line = <DATA> ) { print $line if ( $line =~ m/$pattern/ ); } # using an 'if' block while( my $line = <DATA> ) { if( $line =~ m/$pattern/ ) { print $line; } } __DATA__ somelines... somelines... somewords...keyword1..somewords somelines... somewords... keyword2...somewords...
Both examples print:
pattern = [keyword1|keyword2|keyword3] somewords...keyword1..somewords keyword2...somewords...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: searching for keywords
by ikegami (Patriarch) on Jan 18, 2006 at 03:09 UTC | |
by davido (Cardinal) on Jan 18, 2006 at 06:30 UTC | |
by ikegami (Patriarch) on Jan 18, 2006 at 06:53 UTC | |
by Sioln (Sexton) on Jan 18, 2006 at 07:19 UTC | |
by davido (Cardinal) on Jan 18, 2006 at 08:41 UTC |