Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HELP!!!! - jk need some help removing line returns in a multiline var 1

Status
Not open for further replies.

Eloff

Programmer
Aug 29, 2001
78
CA
okay I have some input from the user in $spy an html document, 91 lines of text. I want to put it all on one line.

@spy = split(/$/m,$spy);
#That seems to split the variable into seperate lines


foreach $item (@spy) {
chop($item);
#should chop off the line returns right? doesn't seem to do anything.
push(@spies,$item);
}

print <<EOF;
@spies
EOF

printed out the other end I now have the html document the user gave me, completely unchanged. It should be printed out on all one line, however.

Any help, is as always, appreciated.

Daniel
 
Hey Daniel,

I always open templates or html files like this,

open(data,&quot;</path/to/folder/file.html&quot;);
while(<data>){
$spy.=$_;
}
close(data);

To remove the line breaks you'll want to do something like,

$spy=~tr/\n\r//d;

I actually don't use that, but It's a little more practical than

$spy=~s~\n|\r~~g;

Which is what I use anyway.

And chop removes the last character, but not always is that a newline, so chomp($item); probably would have been safer, which only eliminates the \n at the end of the line. Sometime, especially on windows, you have \n\r at the end of each line, like me typing in the form box right now, windows adds the \r return carriage as well as the newline so the removal of both the return and newline will guarantee the termination of the line break.

see perldoc.com and search for chop & chomp for more information on them if you want. It's a great resource for all perl syntax questions though.

Tony
 
A lazy way,
[tt]
$spy =~ s/\s*?\n/ /gm;
[/tt]

will remove all whitespace/newline combinations from your variable and replace them with a single space.

Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Ahh thats the thing that \r return, because my users paste the html document into a texarea box like this one. Thnx for the info on chomp as well. I'll try it out immediately, and let you know how it works.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top