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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

new line problem for string

Status
Not open for further replies.

200720082009

Technical User
Joined
Sep 4, 2007
Messages
1
Location
US
Hi,

...
$cotents ='';
...
$newString = 'hello\n';
$cotents .= $newString;

open OUT, ">$FILE" or die "Can not open $FILE for writing :$!";
print OUT $contents;
...


I tried to add a new string (named $newString) with a new line at the end of that string to a string variable ($contents). Then when I tried to write that $contents into an output file. I found the output file actually contains hello\n instead of a 'hello' and a new line. Anybody could help me out how to write a string with a new line to a file?

Thanks.

Mike
 
$newString = 'hello\n';

I think its because you have declared 'hello\n' as a string. You should wrap it in quotations to ensure the \n is evaluated as being a 'new line indicator'. So...

$newString = "hello\n";

I'm really only a beginner at Perl, so forgive me if this isn't what is wrong :).

Chris
 
I would suggest putting "use strict;" and "use warnings;" near the top of all your scripts.

Code:
[red][b]$cotents[/b][/red] ='';
...
$newString = 'hello\n';
[red][b]$cotents[/b][/red] .= $newString;

open OUT, ">$FILE" or die "Can not open $FILE for writing :$!";
print OUT [blue][b]$contents;[/b][/blue]
 
chrissmassey is correct, the '\n' muct be in a double-quoted string to be interpolated as a newline metacharacter and not a literal \ followed by an n.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top