Optimal way to handle variable value search replace 
Author Message
 Optimal way to handle variable value search replace

If you have a variable (say $text_of_email)

and you want to parse through it looking for a specific line (say
"OrdNum=20021")

and then change this to something else (say "Order Number:  20021")

What would be the most optimal way to do this?

The variable has a large amount of text in it and without rewriting a large
part of the app to change the use of it rather than some other method, I
would like to change some of the values within it instead.  Have tried
regexp but am worried that recursive calls would not be optimal.



Mon, 08 Sep 2003 05:38:56 GMT  
 Optimal way to handle variable value search replace

Quote:
> If you have a variable (say $text_of_email)

> and you want to parse through it looking for a specific line (say
> "OrdNum=20021")

> and then change this to something else (say "Order Number:  20021")

> What would be the most optimal way to do this?

I would recommend using the substr function, as in:

    assume $search_text  contains the string to be located and replaced
                   $replacement contains the replacement text

    then:

    # locate the occurrence of string
    my $index = index ( $text_of_email, $search_text );

    # use the replacement version of the 'substr' function
    substr ( $text_of_email, $index, length ( $search_text ), $replacement )
if ( $index >= 0 );

By the way, you didn't specify whether you wanted to replace just the first
occurrence or all occurrences. The above snippet replaces only the first
occurrence. To replace all occurrences you need to place the above code in a
loop that runs as long as it finds the search string. For optimization
purposes, I would maintain the offset of the last occurrence and start the
next search after the end of its replacement (assuming you want to ignore
possible overlaps between the search and replace string. The code might run
as follows:

    my $index = 0;
    my $incr = length ( $replacement );
    while ( ( $index = index($text_of_email, $search_text, $index ) ) >= 0 )
    {
        substr ( $text_of_email, $index, length ( $text_of_email ),
$replacement ) ;
        $index += $incr;
    }

-David



Mon, 08 Sep 2003 20:30:16 GMT  
 Optimal way to handle variable value search replace
Forgot to include the regexp solution that I was successful with:

$thetext = "";


{
  $element =~ s/Order Number/OrdNum/g;
  $thettext .= $element . "\n";

Quote:
}

Going into the above code $thetext has approximately 25 to 100 lines.  So,


using the regexp to change the value of that element.  Then concatenating


Quote:
> If you have a variable (say $text_of_email)

> and you want to parse through it looking for a specific line (say
> "OrdNum=20021")

> and then change this to something else (say "Order Number:  20021")

> What would be the most optimal way to do this?

> The variable has a large amount of text in it and without rewriting a
large
> part of the app to change the use of it rather than some other method, I
> would like to change some of the values within it instead.  Have tried
> regexp but am worried that recursive calls would not be optimal.



Tue, 09 Sep 2003 06:28:48 GMT  
 Optimal way to handle variable value search replace

Quote:

> If you have a variable (say $text_of_email)
> and you want to parse through it looking for a specific line (say
> "OrdNum=20021")
> and then change this to something else (say "Order Number:  20021")
> What would be the most optimal way to do this?

Why not simply

  $text_of_email =~ s/OrdNum=(\d+)/Order Number: $1/gm;

???

--
John Porter



Tue, 09 Sep 2003 00:00:19 GMT  
 Optimal way to handle variable value search replace
[A complimentary Cc of this posting was sent to John Porter


Quote:
> Why not simply

>   $text_of_email =~ s/OrdNum=(\d+)/Order Number: $1/gm;

Why //m ?  Why capture digits?

  $text_of_email =~ s/OrdNum=(?=\d+)/Order Number: /g;

may be much more effective due to the constant replacement string.
Depending on the problem, one may replace this by

  $text_of_email =~ s/OrdNum=/Order Number: /g;

and get something which simply flies (well, it would fly much better
if the replacement string would be equal or shorter than the search
string, but this is tough luck...).

Ilya



Tue, 09 Sep 2003 15:25:48 GMT  
 Optimal way to handle variable value search replace
After getting many suggestions on the individual line search/replace regexp,
which may I add has been great, I wanted to point out which I obviously
didn't before was that I need to go through and look at each line and check
it and possibly change to produce an standard output based on how the user
wants to see it.  My example didn't really state this but OrdNum isn't the
only thing that needs to be checked and changed.

So what is the best way to parse through the variable?  Is it my way of
splitting it to an array and the using for each element to go line by line
as in my second email?  Or some other way that would be better?


Quote:
> Forgot to include the regexp solution that I was successful with:


> $thetext = "";


> {
>   $element =~ s/Order Number/OrdNum/g;
>   $thettext .= $element . "\n";
> }

> Going into the above code $thetext has approximately 25 to 100 lines.  So,

clearing

> using the regexp to change the value of that element.  Then concatenating

array.



> > If you have a variable (say $text_of_email)

> > and you want to parse through it looking for a specific line (say
> > "OrdNum=20021")

> > and then change this to something else (say "Order Number:  20021")

> > What would be the most optimal way to do this?

> > The variable has a large amount of text in it and without rewriting a
> large
> > part of the app to change the use of it rather than some other method, I
> > would like to change some of the values within it instead.  Have tried
> > regexp but am worried that recursive calls would not be optimal.



Sat, 13 Sep 2003 08:58:41 GMT  
 Optimal way to handle variable value search replace
After getting many suggestions on the individual line search/replace regexp,
which may I add has been great, I wanted to point out which I obviously
didn't before was that I need to go through and look at each line and check
it and possibly change to produce an standard output based on how the user
wants to see it.  My example didn't really state this but OrdNum isn't the
only thing that needs to be checked and changed.

So what is the best way to parse through the variable?  Is it my way of
splitting it to an array and the using for each element to go line by line
as in my second email?  Or some other way that would be better?



Sun, 14 Sep 2003 03:54:32 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. TechTip: Understanding LOCAL SHARE

2. using variables as the search/replace values with () and $

3. Searching for tags - Is this optimal

4. :search and replace ascii values

5. search and replace between values

6. syntax for hex value in search and replace

7. Seeking DBBitmap but as DBJpeg component, anyone?

8. Cutting variable length scalar into chunks - optimal?

9. Substitution with parameters and variable don't work s/$search/$replace/i

10. Problem with search/replace and an incremented variable

11. replacing search variable in foreach

12. search/replace with variables

 

 
Powered by phpBB® Forum Software