Working with a line of text ???

Discussion in 'Perl Misc' started by Rodney, Oct 1, 2003.

  1. Rodney

    Rodney Guest

    I have a situation where I want to send portions of a line of text to a
    subroutine for processing. The text line could look like this:

    blah blah <!--Start-- blah blah blah--End--> blah blah blah <!--Start-- blah
    blah--End--> blah blah blah.

    I only want to send the text that is NOT within the <!--Start-- and --End-->
    tags. So example above would have 3 pieces of text the need to run through
    the subroutine.

    After they go through the subroutine, the line of text needs to be regrouped
    in the same order it was before it was processed and the "Tags" have to be
    in tact.

    I've tried "splitting" the line and working with the array elements.... but
    this process seems to take a VERY long time.

    see example below:

    #==================================================start
    sub AddKeyWordsAndNumericConst {
    my ($TextBlockToConvert) = @_;

    my $TextBlockToConvert2 = "";
    my $TextBlockToConvert3 = "";




    ### beginning tag looks like this: <!-- Quoted Text Start QTS-->




    @CodeBreakApart = split(/<!-- Quoted Text Start /,
    $TextBlockToConvert) ;

    for($ArrayTicker3=0; $ArrayTicker3 < @CodeBreakApart; $ArrayTicker3++)
    {
    $CodeBreakTest = $CodeBreakApart[$ArrayTicker3];

    if (not($CodeBreakTest =~ m/QTS-->/g)) {

    $CodeBreakTest = &OpalNumberConstants($CodeBreakTest);
    $CodeBreakTest = &OpalKeywords($CodeBreakTest);

    $CodeBreakApart[$ArrayTicker3] = $CodeBreakTest;
    $CodeBreakTest = "";
    $TextBlockToConvert2 =
    $TextBlockToConvert2.$CodeBreakApart[$ArrayTicker3];
    }
    else {
    $CodeBreakTest = "<!-- Quoted Text Start ".$CodeBreakTest;
    $CodeBreakApart[$ArrayTicker3] = $CodeBreakTest;
    $CodeBreakTest = "";
    $TextBlockToConvert2 =
    $TextBlockToConvert2.$CodeBreakApart[$ArrayTicker3];
    }
    }



    ### end tag looks like this: <!-- Quoted Text End QTE-->



    @CodeBreakApart2 = split(/QTE-->/, $TextBlockToConvert2) ;

    for($ArrayTicker4=0; $ArrayTicker4 < @CodeBreakApart2; $ArrayTicker4++)
    {
    $CodeBreakTest2 = $CodeBreakApart2[$ArrayTicker4];

    if (not($CodeBreakTest2 =~ m/<!-- Quoted Text End /g)) {

    $CodeBreakTest2 = &OpalNumberConstants($CodeBreakTest2);
    $CodeBreakTest2 = &OpalKeywords($CodeBreakTest2);

    $CodeBreakApart2[$ArrayTicker4] = $CodeBreakTest2;
    $CodeBreakTest2 = "";
    $TextBlockToConvert3 =
    $TextBlockToConvert2.$CodeBreakApart2[$ArrayTicker4];
    }
    else {
    $CodeBreakTest2 = $CodeBreakTest2."QTE-->";
    $CodeBreakApart2[$ArrayTicker4] = $CodeBreakTest2;
    $CodeBreakTest2 = "";
    $TextBlockToConvert3 =
    $TextBlockToConvert2.$CodeBreakApart2[$ArrayTicker4];
    }
    }


    return $TextBlockToConvert3;
    }
    #===============================================end


    My question is:

    1) Is there a RegEx statement that would do this.... only faster?
    2. If not, is there a way to make the above process faster?



    Thanks
    --
    ....
    `·.¸¸.·´¯`·.¸¸.·´¯`·-> rodney
    Rodney, Oct 1, 2003
    #1
    1. Advertising

  2. Rodney wrote:
    > I have a situation where I want to send portions of a line of text
    > to a subroutine for processing. The text line could look like
    > this:
    >
    > blah blah <!--Start-- blah blah blah--End--> blah blah blah
    > <!--Start-- blah blah--End--> blah blah blah.
    >
    > I only want to send the text that is NOT within the <!--Start-- and
    > --End--> tags. So example above would have 3 pieces of text the
    > need to run through the subroutine.
    >
    > After they go through the subroutine, the line of text needs to be
    > regrouped in the same order it was before it was processed and the
    > "Tags" have to be in tact.


    This is one idea, assuming the text line is in $_:

    my $start = '<!-- Quoted Text Start QTS-->';
    my $end = '<!-- Quoted Text End QTE-->';

    sub myroutine {
    my $portion = shift;
    # do something with $portion;
    return $portion;
    }

    s/(^|$end)(.*?)($start|$)/$1 . myroutine($2) . $3/egs;

    I don't know about the speed, though.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Oct 1, 2003
    #2
    1. Advertising

  3. Rodney

    Eric Bohlman Guest

    "Rodney" <> wrote in
    news:UEHeb.208$:

    > I have a situation where I want to send portions of a line of text to
    > a subroutine for processing. The text line could look like this:
    >
    > blah blah <!--Start-- blah blah blah--End--> blah blah blah
    > <!--Start-- blah blah--End--> blah blah blah.
    >
    > I only want to send the text that is NOT within the <!--Start-- and
    > --End--> tags. So example above would have 3 pieces of text the need
    > to run through the subroutine.
    >
    > After they go through the subroutine, the line of text needs to be
    > regrouped in the same order it was before it was processed and the
    > "Tags" have to be in tact.


    You might want to take a look at Text::Balanced and see if any of its
    methods would do the trick.
    Eric Bohlman, Oct 3, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hugo
    Replies:
    10
    Views:
    1,290
    Matt Humphrey
    Oct 18, 2004
  2. kaushikshome
    Replies:
    4
    Views:
    752
    kaushikshome
    Sep 10, 2006
  3. scad
    Replies:
    23
    Views:
    1,139
    Alf P. Steinbach
    May 17, 2009
  4. Marek Stepanek
    Replies:
    12
    Views:
    399
    Peter J. Holzer
    Sep 2, 2006
  5. Replies:
    2
    Views:
    133
Loading...

Share This Page