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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.