amir_e_a has asked for the wisdom of the Perl Monks concerning the following question:
Hello,
Consider this code:
use strict; use warnings; my $BEL_LETTERS = qr/ABVHD/; my @all_words = qw(tak het hen toj tah tam tym); ... while (my $next_line = <$FH>) { foreach my $next_word (@all_words) { my $count_words = ($next_line =~ s/\b($next_word[$BEL_LETTERS]*)\b/>$1</gi); } }
The substitution looks for words that begin with any of @all_words and end with zero or more of $BEL_LETTERS.
This code doesn't compile. Problem: the compiler thinks that $next_word[$BEL_LETTERS] is member $BEL_LETTERS of array @next_word.
This works - notice the parentheses:
s/\b($next_word([$BEL_LETTERS])*)\b/>$1</gi);
...But it feels like a hack to me. Is there a better way to overcome this interpolation?
I tried searching for the answer here in Q&A and in the Camel book, and strangely couldn't find it. I'm quite sure that the answer must be in the book, and if someone can point it to the page i'll be very thankful too.
(If anyone is curious, this code is looking for pronouns in a Belarusian text. This example here is very simplified - i didn't want to put the whole Belarusian-Cyrillic alphabet here).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: variables in regex character classes
by Hue-Bond (Priest) on Jul 22, 2006 at 19:48 UTC | |
|
Re: variables in regex character classes
by Joost (Canon) on Jul 22, 2006 at 19:50 UTC | |
|
Re: variables in regex character classes
by Ieronim (Friar) on Jul 22, 2006 at 20:20 UTC | |
by amir_e_a (Hermit) on Jul 23, 2006 at 14:37 UTC | |
by Ieronim (Friar) on Jul 23, 2006 at 17:32 UTC |