Gossamer Forum
Home : General : Perl Programming :

converting text strings in perl script

Quote Reply
converting text strings in perl script
Trying to change text strings using perl script.

source text|replaced text.

Gretna|Dumfries and Galloway
Gretna Green|Dumfries and Galloway
Dumfries|Dumfries and Galloway

but am getting results like these:

Dumfries and Galloway
Dumfries and Galloway Green
Dumfries and Dumfries And Galloway


Obviously, this is occurring because the script is looking at each word and replacing it, even when it is part of a phrase or part of a replacement.
ie:
'Gretna' is to be replaced in its own right and is picked up in 'Gretna Green'; being replaced before 'Green' is seen.
'Dumfries' is replaced by 'Dumfries and Galloway' but the 'Dumfries' is being picked up again and replaced for a second time.

I need to somehow tell the script only to replace the whole phrase.

I've tried using the following but it doesn't work

"Gretna"|"Dumfries and Galloway"
"Gretna Green"|"Dumfries and Galloway"
"Dumfries"|"Dumfries and Galloway"

["Gretna"]|["Dumfries and Galloway"]
["Gretna Green"]|["Dumfries and Galloway"]
["Dumfries"]|["Dumfries and Galloway"]

Can anyone tell me the correct code?
Thanks.
Quote Reply
Re: [Alba] converting text strings in perl script In reply to
I'm not entirely sure what you are asking. Are you looking for a regex?

Quote:
"Gretna"|"Dumfries and Galloway"
"Gretna Green"|"Dumfries and Galloway"
"Dumfries"|"Dumfries and Galloway"

["Gretna"]|["Dumfries and Galloway"]
["Gretna Green"]|["Dumfries and Galloway"]
["Dumfries"]|["Dumfries and Galloway"]

What is the above?
Quote Reply
Re: [Hargreaves] converting text strings in perl script In reply to
Hi

The lines
Quote:

Gretna|Dumfries and Galloway
Gretna Green|Dumfries and Galloway
Dumfries|Dumfries and Galloway

are sample lines of text replacements to be made.

What I am looking for is some way of coding the script so that the lines are read as phrases rather than individual words.

Whether this needs a regex or coding to be added to the lines I do not know...

Thanks
Quote Reply
Re: [Alba] converting text strings in perl script In reply to
There are much more eloquent ways of doing this, but try:

if ($whatever =~ /(Gretna|Dumfries)/) {
$whatever =~ s/$whatever/Dumfries and Galloway/g;
}

or

if ($whatever =~ /(Gretna|Dumfries)/) {
$whatever =~ s/$1/Dumfries and Galloway/g;
}

either one seems to work for me...
Quote Reply
Re: [Watts] converting text strings in perl script In reply to
Thanks, I'll try it out tomorrow. Someone also suggested changing the order of the replacements might help although it couldn't totally solve it.
Quote Reply
Re: [Alba] converting text strings in perl script In reply to
Something like this perhaps?

Code:
my $MAP = {
'Gretna(?!\s+Green)' => 'Dumfries and Galloway',
'Gretna Green' => 'Dumfries and Galloway',
'Dumfries' => 'Dumfries and Galloway'
};

while (my ($replace, $with) = each %$MAP) {
$string =~ s/$replace/$with/ig;
}

Last edited by:

Hargreaves: Nov 15, 2005, 10:14 AM
Quote Reply
Re: [Hargreaves] converting text strings in perl script In reply to
Eventually solved this, thanks everyone for your help.

The main part of the solution was making sure that the source text lines were in reverse alphabetical order.