CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
replace substrings from array

 



BenjaminB
New User

Jul 31, 2006, 4:16 AM

Post #1 of 5 (422 views)
replace substrings from array Can't Post

hi forum users,

I'm reading product rows from a database with a title and a description. After that i'll have to apply some kind of word filter to replace certain words. This is a list of ~600 words also stored in a database like this ('old_word','new_word').

What would be the best approach with performance in mind, to do a task like this? Should i iterate through this array everytime i output a product title or description? Or does anyone has another nifty idea?

This has to be done dynamically so i think i'll have to iterate everytime i output a title or description. If i do so, is it really the best approach to iterate through the wordfilter array and make a substitution on each word?

Thanks in advance
regards Benjamin


(This post was edited by BenjaminB on Jul 31, 2006, 4:21 AM)


davorg
Thaumaturge / Moderator

Jul 31, 2006, 5:29 AM

Post #2 of 5 (412 views)
Re: [BenjaminB] replace substrings from array [In reply to] Can't Post

It's hard to kn ow what you're doing without seeing a concreate example.

In general, I'd point out that 600 records really isn't very big and that you're probably best advised to read that data into memory each time you run the program. It's probably a good idea to read them into a hash where the old word is the key and the new word is the associated value.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


BenjaminB
New User

Jul 31, 2006, 5:35 AM

Post #3 of 5 (411 views)
Re: [davorg] replace substrings from array [In reply to] Can't Post

What i'm trying to approach is quite simple. I want to replace every instance of every word in this wordlist.

So if i have a list like this:
'Eric' => 'Erich'
'Tom' => "Roger"
'food' => 'feet'

and a string like this:
"Eric is sitting in a tree eating Tom's food"

The result should be this:
"Erich is sitting in a tree eating Roger's feet"

Regards Benjamin


davorg
Thaumaturge / Moderator

Jul 31, 2006, 5:44 AM

Post #4 of 5 (409 views)
Re: [BenjaminB] replace substrings from array [In reply to] Can't Post

So a basic approach would be something like:


Code
# Read this in from the database 
my %trans = (
'Eric' => 'Erich',
'Tom' => "Roger",
'food' => 'feet',
);

my $text = q(Eric is sitting in a tree eating Tom's food);
foreach (keys %trans) {
$text =~ s/\b$_\b/$trans{$_}/;
}

print $text;


Note I put used \b in the regex to ensure that we only change whole words.

A more sophisticated approach would be to build up a regex containing all of the words to be replaced.


Code
my %trans = ( 
'Eric' => 'Erich',
'Tom' => "Roger",
'food' => 'feet'.
);

my $regex = '\b(' . join('|', keys %trans) . ')\b';

my $text = q(Eric is sitting in a tree eating Tom's food);

$text =~ s/$regex/$trans{$1}/g;

print $text;


You could use a benchmark to see which is the fastest.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


BenjaminB
New User

Jul 31, 2006, 5:49 AM

Post #5 of 5 (408 views)
Re: [davorg] replace substrings from array [In reply to] Can't Post

I'll check it out. Thanks for your help.

Regards Benjamin

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives