Printing a new line

K

kronecker

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.
 
J

Jens Thoms Toerring

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
 
B

Ben Morrow

Quoth (e-mail address removed):
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
 
J

Jürgen Exner

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
 
A

A. Sinan Unur

(e-mail address removed) wrote in (e-mail address removed):
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


--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

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

kronecker

(e-mail address removed) wrote in (e-mail address removed):




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 "$_\n" for ( $first_name, $last_name );


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




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

--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

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

I tried this and it gave me errors

print example ($first_name\n);
print example ($last_name\n);
 
J

Jürgen Exner

(e-mail address removed) wrote in news:1581fc33-6137-44a8-b363- [inserting line break in output]
I have tried putting \n but it gives errors.

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

I tried this and it gave me errors

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

Well, the way you wrote that the second argument to print is an
expression, similar to e.g.
print example ($foo + 35);
except that instead of the plus operator you were using the backslash
operator which creates a reference to n. And that doesn't make any sense
for several reasons.

\n represents a line break _INSIDE_OF_A_DOUBLE-QUOTED_STRING_ but of
course not when used as a standalone term.
So just use a double-quoted string:
print example "$first_name\n";
print example "$last_name\n";

Of course you can do that in a single print statement, too:
print example "$first_name\n$last_name\n";

jue
 
A

A. Sinan Unur

(e-mail address removed) wrote in


Don't quote sigs.
I tried this and it gave me errors

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

You should not post questions if you are not going to bother to read the
responses.

Sinan
--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

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

Eric Pozharski

A. Sinan Unur said:
(e-mail address removed) wrote in (e-mail address removed):
*SKIP*
print $example "$_\n" for ( $first_name, $last_name );

Me thinks that would be a bit clearer (and resolvels OP's problem (for
this time))

print $example <<"END_OF_TEXT";
$first_name
$last_name
END_OF_TEXT

*CUT*
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,114
Latest member
GlucoPremiumReview
Top