Working with a line of text ???

R

Rodney

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
 
G

Gunnar Hjalmarsson

Rodney said:
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.
 
E

Eric Bohlman

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top