Remove carrige return line feed from a string

I have a $variable received from a HTML textarea form. EG:

This
is
a
test

I want to remove the newlines (carrige return line feeds)from it, so that it looks like:
This is a test
I have tried $variable =~ tr/\n/ /;
BUT IT DOES NOT SEEM TO WORK ????????
 
haastAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kim RyanIT ConsultantCommented:
# search and replace CR with nothing
$variable =~ s/\n//;
0
jhurstCommented:
Actually I would be happy for haast to get the points - he is so near.  But you need:
$variable =~ s/\n//g; #globally replace all line-feeds with nothing
$variable =~ s/\r//g; #globally replace all carriage-returns with nothing
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kim RyanIT ConsultantCommented:
Global flag is not needed as the sample data only conains one CR per line.
\n is the new line delimiter for your operating system, meaning LF for Unix or CR/LF for Windows/DOS, there should be no need to explicitly remove carriage returns.

jhurst, the normal etiquitte here is to submit answers as comments and let the questioner decide which ones to accept.
0
ozoCommented:
Why do you say that $variable =~ tr/\n/ /; does not seem to work?
0
haastAuthor Commented:
teraplane thank you for your suggestion (it did not solve the problem), jhurst is 100% correct the g flag is ESSENTIAL and also besides the =~ s/\n// the =~ s/\r// is ESSENTIAL too.

And I concur with the comment on etiquitte, however, I do understand enthusiasm, so let us all learn from this.

Best Regards,
haast.

PS: ozo you are not forgotten as a potential helper as well.

Thank you ALL.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.