Click here to get back home

Printing a new line

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Printing a new line kronecker 05-10-2008
|--> Re: Printing a new line Jens Thoms Toer...05-10-2008
Posted by kronecker on May 10, 2008, 5:28 pm
Please log in for more thread options
Sounds simple enough.

I am printing to a text file file

open (example, ">remote.txt") || die ("Could not open file. $!");
print example ($first_name);
print example ($last_name);
close (example);

I have tried putting \n but it gives errors. Basically I want the
first and last names on a new line.

thanks

k.



Posted by Jens Thoms Toerring on May 10, 2008, 5:55 pm
Please log in for more thread options
kronecker@yahoo.co.uk wrote:
> I am printing to a text file file

> open (example, ">remote.txt") || die ("Could not open file. $!");

What about checking that open() did succeed?;-)

> print example ($first_name);
> print example ($last_name);
> close (example);

> I have tried putting \n but it gives errors.

It typically is useful if you tell

a) the exact code you did try ("I have tried putting \n" is rather
vague and could mean a lot of different things)
b) the exact error messages you got.

> Basically I want the first and last names on a new line.

print example "$first_name\n";
print example "$last_name\n";

or, even shorter

print example "$first_name\n$last_name\n";

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de

Posted by Ben Morrow on May 10, 2008, 6:01 pm
Please log in for more thread options

Quoth kronecker@yahoo.co.uk:
> Sounds simple enough.
>
> I am printing to a text file file
>
> open (example, ">remote.txt") || die ("Could not open file. $!");
> print example ($first_name);
> print example ($last_name);
> close (example);
>
> I have tried putting \n but it gives errors. Basically I want the
> first and last names on a new line.

You really need to find and read a basic Perl book. There are two ways
of doing this: print a newline explicitly, like this:

print example ("$first_name\n");

and ask perl to add one to the end of every print statement like this:

$\ = "\n";

print example ($first_name);
print example ($last_name);

Some further points: it's usual in Perl code to make filehandles
all-caps, so they stand out. Also, it's much better to use filehandles
that live in variables than the old global filehandles, and it's much
safer to get into the habit of using the 3-argument form of open, like
this:

open(my $EXAMPLE, '>', 'file.txt')
|| die("can't open file,txt: $!");

Ben

--
Every twenty-four hours about 34k children die from the effects of poverty.
Meanwhile, the latest estimate is that 2800 people died on 9/11, so it's like
that image, that ghastly, grey-billowing, double-barrelled fall, repeated
twelve times every day. Full of children. [Iain Banks] ben@morrow.me.uk

Posted by Jürgen Exner on May 10, 2008, 6:02 pm
Please log in for more thread options
kronecker@yahoo.co.uk wrote:
>open (example, ">remote.txt") || die ("Could not open file. $!");
>print example ($first_name);
> print example ($last_name);
>close (example);
>
>I have tried putting \n

How?

>but it gives errors.

Which?

>Basically I want the
>first and last names on a new line.

Well, yes, adding a "\n" in the output stream will do exactly that under
normal circumstances.

jue

Posted by A. Sinan Unur on May 10, 2008, 6:09 pm
Please log in for more thread options
kronecker@yahoo.co.uk wrote in news:1581fc33-6137-44a8-b363-
d15a016988ab@d19g2000prm.googlegroups.com:

> Sounds simple enough.
>
> I am printing to a text file file
>
> open (example, ">remote.txt") || die ("Could not open file. $!");

The recommended convention, if you are going to use bareword
filehandles, is to use all upper case. On the otherhand, such
filehandles are package local which means they can be the source of
hard-to-track bugs. I would recommend:

my $remote_file = 'remote.txt';

open my $example, '>', $remote_file
or die "Cannot open '$remote_file': $!";

> print example ($first_name);
> print example ($last_name);

print $example "$_\n" for ( $first_name, $last_name );

> close (example);

Check on errors on close as well, especially if you opened the file for
writing.

>
> I have tried putting \n but it gives errors.

This is not helpful. Where did you try to put \n?

If you are using 5.10, you can just use the new say function to append a
newline automatically to every line you print.

#!/usr/bin/perl

use strict;
use warnings;

use 5.010;

my ($first_name, $last_name) = qw( first last );

my $remote_file = 'remote.txt';

open my $example, '>', $remote_file
or die "Cannot open '$remote_file': $!";

say $example $_ for ( $first_name, $last_name );

close $example
or die "Error closing '$remote_file': $!";

__END__

C:\Temp> t

C:\Temp> cat remote.txt
first
last


--
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/

Similar ThreadsPosted
printing a subject line October 16, 2007, 10:36 pm
backtick not printing the 'echo'ed line July 19, 2004, 12:59 pm
start printing at the end of the previous line July 18, 2006, 10:37 am
Printing the next line of text of the file April 19, 2007, 6:37 pm
Beginner: read $array with line breaks line by line August 27, 2006, 9:40 am
FAQ 5.2: How do I change one line in a file/delete a line in a file/insert a line in the middle of a file/append to the beginning of a file? December 7, 2004, 6:03 am
FAQ 5.2 How do I change one line in a file/delete a line in a file/insert a line in the middle of a file/append to the beginning of a file? January 27, 2005, 12:03 pm
FAQ 5.2 How do I change one line in a file/delete a line in a file/insert a line in the middle of a file/append to the beginning of a file? April 17, 2005, 11:03 am
FAQ 5.2 How do I change one line in a file/delete a line in a file/insert a line in the middle of a file/append to the beginning of a file? July 3, 2005, 10:03 am
FAQ 5.2 How do I change one line in a file/delete a line in a file/insert a line in the middle of a file/append to the beginning of a file? September 10, 2005, 4:03 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap