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: Need a Custom or Prewritten Perl Program?: I need a program that...:
Replaces a piece of text!

 



speedman
New User

Jun 17, 2008, 12:21 AM

Post #1 of 2 (2424 views)
Replaces a piece of text! Can't Post

Hi all,

I'm a new perl user and I'm studing the basis of language.

I saw various constructs to replace a string as : s// or m//.

But I'm tryng to replace a piece of text.

For example : I've



To be, or not to be: that is the question:
Whether 'tis nobler in the mind to suffer
The slings and arrows of outrageous fortune,
Or to take arms against a sea of troubles,
And by opposing end them? To die: to sleep;
No more; and by a sleep to say we end
The heart-ache and the thousand natural shocks
That flesh is heir to, 'tis a consummation
Devoutly to be wish'd. To die, to sleep;
To sleep: perchance to dream: ay, there's the rub;
For in that sleep of death what dreams may come
When we have shuffled off this mortal coil,
Must give us pause: there's the respect
That makes calamity of so long life;

and I want

To be, or not to be: that is the question:
Whether 'tis nobler in the mind to suffer
The slings and arrows of outrageous fortune,
Or to take arms against a sea of troubles,
And by opposing end them? To die: to sleep;
To sleep: perchance to dream: ay, there's the rub;
For in that sleep of death what dreams may come
When we have shuffled off this mortal coil,
Must give us pause: there's the respect
That makes calamity of so long life;



eliminating the bol phrase. I think at a for and next loop, but I don't known to do it.

Can you help me ?



Thank you in advance and sorry for my poor english!



speedman!


meloyelo
User

Jun 18, 2008, 3:52 PM

Post #2 of 2 (2376 views)
Re: [speedman] Replaces a piece of text! [In reply to] Can't Post

I advice is to:

1. Slurp in the text into a string (see File::Slurp)
2. Perform a s{}{} command (see example below)
3. Print out the result.

Example:

Code
my $text = "This is some text 
that has many lines.
blah blah blah
whatever whatever
blah
This is the end of the text.";

$text =~ s{^blah blah blah
whatever whatever
blah
}{}gm;

print $text;


 
 


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

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