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:
Substitution of a string

 



Cheral
stranger

Nov 2, 2001, 12:29 PM

Post #1 of 5 (723 views)
Substitution of a string Can't Post

I need a code that would substitute every occurrence of the string "TH" with the string "Th". It should print the entire file to standard output, not just replace lines.

Thanks for your help!



Coderifous
journeyman

Nov 2, 2001, 9:06 PM

Post #2 of 5 (715 views)
Re: Substitution of a string [In reply to] Can't Post

well, do you really want every occurence of TH to be replaced w/ Th?

JANE HATHAWAY -> JANE HAThAWAY
THANKS THELONIUS ->ThANKS ThELONIUS
WOULDST THOU HATH -> WOULDST ThOU HATh

or do you want every TH tag in some document to be replaced with Th?

If the latter is true (and I think it is) then use this line:

Code
 
$variable_with_the_THs =~ s/\bTH\b/Th/g;


Hope this helps Wink

--Jim





Coderifous
journeyman

Nov 2, 2001, 9:08 PM

Post #3 of 5 (715 views)
Re: Substitution of a string [In reply to] Can't Post

Oh yea, I forgot, you wanna output the result:

Code
 
print $variable_with_the_THs;



--Jim



Cheral
stranger

Nov 5, 2001, 4:36 AM

Post #4 of 5 (698 views)
Re: Substitution of a string [In reply to] Can't Post

I want every occurance of TH in the file to be replaced with Th. It should print the entire file to standard output, not just replace lines.


Thanks




fashimpaur
User / Moderator

Nov 5, 2001, 4:49 AM

Post #5 of 5 (697 views)
Re: Substitution of a string [In reply to] Can't Post

Okay. Then do this:

open (DATA, "yourfile.txt");
while (<DATA>){
s/TH/Th/g;
print;
}
close DATA;
exit;

HTH,

Dennis

$a="c323745335d3221214b364d545a362532582521254c3640504c37292f493759214b3635554c3040606a0",print unpack"u*",pack "h*",$a,"\n\n"

 
 


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

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