Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have this code in my program and it's parsing a large log file. When it reaches at this point the regular expression seems to hang badly, there is a faster way to process the code to improve speed? Here is the code:
my $p1= "iapw_p1"; my $p2= "iapw_p2"; my $p3= "iapw_p3"; my $b1= "iapw_b1"; my $p0= "iapw_p0"; my $c0= "iapw_c0"; my $c1= "iapw_c1"; my $c3= "iapw_c3"; my $h1= "iapw_h1"; my $p1b="<font color=\"navy\" size=\"2\"><b>P1</b></font><font color=\ +"#000000\" size=\"2\"><b><i> = (file inquiry - launch page)</i></b></ +font>"; my $p2b="<font color=\"navy\" size=\"2\"><b>P2</b><font color=\"#00000 +0\" size=\"2\"><b><i> = (file search - replace - operators)</i></b></ +font>"; my $p3b="<font color=\"navy\" size=\"2\"><b>P3</b><font color=\"#00000 +0\" size=\"2\"><b><i> = (file notepad)</i></b></font>"; my $b1b="<font color=\"navy\" size=\"2\"><b>B1</b><font color=\"#00000 +0\" size=\"2\"><b><i> = (file inquiry: old - owners and preowners)</i +></b></font>"; my $p0b="<font color=\"navy\" size=\"2\"><b>P0</b><font color=\"#00000 +0\" size=\"2\"><b><i> = (file search)</i></b></font>"; my $c0b="<font color=\"navy\" size=\"2\"><b>C0</b><font color=\"#00000 +0\"e size=\"2\"><b><i> = (file search)</i></b></font>"; my $c1b="<font color=\"navy\" size=\"2\"><b>C1</b><font color=\"#00000 +0\" size=\"2\"><b><i> = (file inquiry)</i></b></font>"; my $c3b="<font color=\"navy\" size=\"2\"><b>C3</b><font color=\"#00000 +0\" size=\"2\"><b><i> = (file notepad)</i></b></font>"; my $h1b="<font color=\"navy\" size=\"2\"><b>H1</b><font color=\"#00000 +0\" size=\"2\"><b><i> = (file and arch inquiry and history)</i></b></ +font>"; for (@a_data) { s/$p1/$p1b/; s/$p2/$p2b/; s/$p3/$p3b/; s/$b1/$b1b/; s/$p0/$p0b/; s/$c0/$c0b/; s/$c1/$c1b/; s/$c3/$c3b/; s/$h1/$h1b/; }

edited: Wed Aug 14 15:08:05 2002 by jeffa - title change (was: Regular Expression)

Replies are listed 'Best First'.
Re: Regular Expression
by Abigail-II (Bishop) on Aug 13, 2002 at 16:23 UTC
    Untested:
    s!iapw_(..)!qq {<font color = "navy" size = "2"><b>\U$1\E} . qq {</b><font color = "#000000" size = "2"><b><i>} . qq { = (file inquiry: old - owners and preowners)} . qq {</i></b></font>}!eg for @a_data;
    The only reason for the /e and the qq{} is readability.

    Abigail

      Seems the text in the parens changes slightly for each of his sub strings. I changed Abigail-II's example to take this into acccount.
      my %text = ( p1 => '(file inquiry - launch page)', p2 => '(file search - replace - operators)', p3 => '(file notepad)', b1 => '(file inquiry: old - owners and preowners)', p0 => '(file search)', c0 => '(file search)', c1 => '(file inquiry)', c3 => '(file notepad)', h1 => '(file and arch inquiry and history)'); s!iapw_(\w\d)!qq {<font color = "navy" size = "2"><b>\U$1\E} . qq {</b><font color = "#000000" size = "2"><b><i>} . qq { = $text{$1}</i></b></font>}!eg for @a_data;

      (Oh, I also tested it -- it works!)
      Cheers,
      Shendal
        It did work just like the first code I have. If I am using a small file it goes fast, but if I am parsing a text log file with 100.000 lines it hangs again!