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: Beginner:
Problem substituting for words which begin or end with delimiters

 



LeoSimon
New User

May 21, 2013, 11:21 AM

Post #1 of 5 (443 views)
Problem substituting for words which begin or end with delimiters Can't Post

Hi

I inherited the attached code, called "perlSubst", below from a colleague.

The syntax is "perlSubst <from> <to> <filename>"

I want to convert occurances of {a} in the file "foo" to {b}

The following works fine if {a} is preceded and followed by a character in the file foo.

perlSubst '{a}' '{b}' foo

So if for example, foo is the line

a{a}a

the command works fine.

But if foo is the line

{a}

it doesn't work.

Similarly, if foo is the line

a{a}

then the command

perlSubst '{a' '{b' foo

will work, but again

perlSubst '{a}' '{b}' foo

doesn't make the substitution

So my problem is specifically with words that begin or end with delimiters like {}. The same problem happens with ().

Any help would be *most* appreciated.
Attachments: perlSubst (0.23 KB)


Laurent_R
Veteran / Moderator

May 21, 2013, 12:00 PM

Post #2 of 5 (438 views)
Re: [LeoSimon] Problem substituting for words which begin or end with delimiters [In reply to] Can't Post

Not sure of what you want to do exactly, but the quotemeta function might help you.


BillKSmith
Veteran

May 21, 2013, 1:37 PM

Post #3 of 5 (432 views)
Re: [LeoSimon] Problem substituting for words which begin or end with delimiters [In reply to] Can't Post

Your script is designed to substitute 'words', not arbitrary character strings. It only makes the substitution if a match occurs between word boundaries. Perl's idea of a word can get complicated. Do not expect your script to work correctly if your 'from' contains any character except an alphanumeric or an underscore.

It is probably easier to write a separate script to handle this case than to modify the existing one. (The hard part would be proving that your change does not affect anything else that might matter.)
Good Luck,
Bill


LeoSimon
New User

May 22, 2013, 12:37 AM

Post #4 of 5 (418 views)
Re: [Laurent_R] Problem substituting for words which begin or end with delimiters [In reply to] Can't Post

Thanks very much, quotemeta does exactly what I wanted!!


BillKSmith
Veteran

May 22, 2013, 5:44 AM

Post #5 of 5 (409 views)
Re: [LeoSimon] Problem substituting for words which begin or end with delimiters [In reply to] Can't Post

My warning still stands. At the very least keep the original script. I expect that you will find special cases where you need it.
Good Luck,
Bill

 
 


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

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