hi
i am using win32 GUI 1.06 with perl 5.10
in the following code i have a RichEdit control, i want to read a big file and to store it in @a array in which every element store one line.
then i want to find every occurences of 'be' pattern and to color it in Red color. my code are working fine, but if the 'be' occurrences are numerous and the file also is big, the process of coloring take too much time.
can someone please look how i could optimise the code?
Thanks
use Win32::GUI; use warnings; use strict; my $Win = new Win32::GUI::Window( -left => 3, -top => 5, -width => 640, -height => 480, -name => "Win", ); $Win->Show(); my $font = Win32::GUI::Font->new( -name => "Courier New", -size => 12, ); $Win->AddButton( -text => "Run", -name => "Button1", -left => 23, -top => 10, -width => 56, -height => 36, -foreground => 0, ); my $RichText = $Win->AddRichEdit( -text => "", -name => "RichText_Output", # OutPut Rich Text -left => 41, -top => 91, -width => 580, -height => 313, -multiline => 1, -vscroll => 1, ); $RichText->SetLimitText(4194304); Win32::GUI::Dialog(); sub Win_Terminate { return -1; } sub Button1_Click { my $file = "bigfile.txt"; my $onelineOfText; my @ptrns; my $outString; open(MYFILE, $file) || die; my @a = <MYFILE>; chomp @a; my $PATTERN = 'be'; for (@a) { $onelineOfText = $_; @ptrns = $onelineOfText =~ m/$PATTERN/g; if($ptrns[0]) { $outString = $outString . "$onelineOfText\r\n"; } } # print the lines containing patterns to RichText: $RichText->SetSel(-1,-1); # Move insertion point to the end $RichText->ReplaceSel($outString); #Insert at the insertion point # now the coloring process of just the matches: my $OutStrng = $RichText->Text(); #get the contents of RichText my @pstn; my $flag = 1; #get the positions of the beginning and end of every match: #and store them in a continious array @pstn while ($flag) { if($OutStrng =~ /$PATTERN/g){ push @pstn, $-[0]; push @pstn, $+[0]; } else {last} } my $x = 0; my $lng = $#pstn / 2; for(0..$lng){ $RichText -> SetSel($pstn[$x],$pstn[$x+1]); $RichText -> SetCharFormat( -color => "#FF0000"); $x = $x + 2; }; }

In reply to coloring text in RichEdit takes long time by orange

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.