gryphon has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: foreach (@array) s/x/y/ efficiency
by chipmunk (Parson) on Jan 10, 2001 at 08:06 UTC | |
by gryphon (Abbot) on Jan 11, 2001 at 02:43 UTC | |
by chipmunk (Parson) on Jan 11, 2001 at 03:36 UTC | |
by gryphon (Abbot) on Jan 11, 2001 at 04:15 UTC | |
|
Re: foreach (@array) s/x/y/ efficiency
by merlyn (Sage) on Jan 10, 2001 at 06:12 UTC | |
by gryphon (Abbot) on Jan 11, 2001 at 03:28 UTC | |
|
Re: foreach (@array) s/x/y/ efficiency
by repson (Chaplain) on Jan 10, 2001 at 08:51 UTC | |
by gryphon (Abbot) on Jan 11, 2001 at 02:53 UTC |