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.
> 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 ___ j...@toerring.de
\__________________________ http://toerring.de
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
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] b...@morrow.me.uk
> 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 <1u...@llenroc.ude.invalid>
(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);
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
< snip full quote ... you didn't read what I wrote anyway >
>> --
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 <1u...@llenroc.ude.invalid>
*SKIP*
>> print example ($first_name);
>> print example ($last_name);
> 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*
--
Torvalds' goal for Linux is very simple: World Domination