nathaniels has asked for the wisdom of the Perl Monks concerning the following question:
Hello All,
I'm trying to create an array of regexp matches in context. What I have below clearly doesn't work as the special variables are only initialized to the first match. I've tried using a while loop but I keep getting stuck in infinite loops. What's the best way of doing this?
foreach my $line (@lines) { foreach ( lc ($line) =~ /\b $token \b/g ) { $before_string = $`; $after_string = $'; if ( length $before_string > 55 ) { $before_string = substr( $before_string, -55 ); } if ( length $after_string > 55 ) { $after_string = substr( $after_string, 0, 55 ); } push @contexts, [$before_string, $token, $after_string]; } }
Thanks!
UPDATE: Thank you all! I Have learned a lot!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Special Variables and Multiple Regexp Matching
by GrandFather (Saint) on Nov 18, 2013 at 00:39 UTC | |
|
Re: Special Variables and Multiple Regexp Matching
by Kenosis (Priest) on Nov 18, 2013 at 01:44 UTC | |
by nathaniels (Acolyte) on Nov 19, 2013 at 04:15 UTC | |
by Kenosis (Priest) on Nov 19, 2013 at 06:13 UTC | |
|
Re: Special Variables and Multiple Regexp Matching
by kcott (Archbishop) on Nov 18, 2013 at 02:13 UTC | |
|
Re: Special Variables and Multiple Regexp Matching
by LanX (Saint) on Nov 18, 2013 at 00:30 UTC | |
|
Re: Special Variables and Multiple Regexp Matching
by AnomalousMonk (Archbishop) on Nov 18, 2013 at 16:26 UTC |