Greetings all,
I have an array called @material that contains a text file. I've also got an array called @key_phrases where each element is a string with two words in it, seperated by a space. Each element represents a two-word phrase in the text file that exists only once, thereby making it "key."
What I want to do is find the most efficient way, without loosing the structure of @material, of finding and "marking" each key words pair with HTML bold tagging. What I've come up with works, but it's painfully slow.
my $phrase; foreach $phrase (@key_phrases) { my($worda,$wordb) = split(/ /, $phrase); foreach (@material) { last if (s|([\W\b])($worda)(\W+)($wordb)([\W\b])| $1<B>$2</B>$3<B>$4</B>$5|i); } } # Here's where I clean up redundant tags... foreach (@material) { s|<B><B>|<B>|g; s|</B></B>|</B>|g; s|</B> <B>| |g; }
Any suggestions on how to get this to run faster? It annoys me that I have to loop through @material each time I need to find a @key_phrases element. By definition, the key phrase will only happen once in @material, so I could do some kind of grep as long as I can put the altered element back into @material in the right order.
Oh, one other thing, I'm running this on a Win32 box (not by choice), so I'm somewhat restricted in what UNIX command-line features I can use.
Thanks in advance. :)
-Gryphon.
In reply to foreach (@array) s/x/y/ efficiency by gryphon
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |