View Full Version : matching a word of text with an array item.

05-03-2010, 01:28 PM

I ahve a section of text like this

The quick brown fox jumps over the lazy dog.

My array is like this
@array = 'brown','lazy';

My attempt is to use grep but I think I am trying to use the wrong structure.

my @text = $text;

foreach my word (@text){
if (grep $word, @text){
do this;
} else {
do that;

Should I put the text into an array and grep on each of its words to try to match the @array?


05-05-2010, 02:10 PM

Are you still needing help with this, or have you worked out the solution?

If you need help, then please provide more info on what you need to accomplish. The code you posted is not doing what you think, so before I make any suggestions, I'd like to have a little clarification on what you need.

05-05-2010, 03:00 PM
FishMonger; yes, I am still struggling. I have prgressed enough to realise I should be using a regex but I am not even sure that this is the best way.

what if, say, I have a word like 'London' and a phrase like 'London Airport'. I need to show a different link for each of those scenarios.

so far my thoughts have reached this point.

pseudo code:

foreach my $word_or_phrase (@array_of_link_words){
$href = "<a href='/example.com/script/$word_or_phrase'>$word_or_phrase</a>";
$text =~ s/ $word_or_phrase/$href/g; #space should ensure only plain text can be converted. Don't want a link to London Airport, being mashed up bby the single word London.

Is this anyway close?