Andreas Karlsson wrote:
> Hi guys!
> When writing to a file there is a space inserted in
> front of every row except the first one.
> Why is that and how do I get rid of it?
> Any nice soul out there who knows?!?
> 
> I've had this problem with all my scripts for
> several years but now I cannot live with it
> anymore...;)
> 
> Does it have something to do with me running in a
> windows environment? (Win XP)

No. See comments in your code.

> I post an example program which generates a new file
> from another.
> The file dummy_file.txt can be any simple textfile.
> When comparing dummy_file.txt with
> new_dummy_file.txt
> one can see the spaces inserted on every row but the
> first.
> For example it looks like this in the dummy_file.txt
> row1
> row2
> row3
> And then like this in the new_dummy_file.txt
> row1
>  row2
>  row3
> 
> brgds
> Andreas
> 
> #!/usr/local/bin/perl
> #
> # Program to do the obvious ;)
> #useage: perl -w read_write_test.pl

Perl will see the -w if you put it on the #! Line, or if your perl is
recent enough you can "use warnings;". Saves having to remember it every
time you run a script.

> 
> use strict;
> use strict 'refs';

This is redundant. It is implied by "use strict".

> $|=1; #Flush ON

Why? I can't see anything in this script that requires it.

> # Print a message
> print "Script started.\n";
> 
> #open a file to read from
> open(INPUT, "dummy_file.txt") || die "Error opening
> file dummy_file.txt $!\n";
> my(@input_lines) = <INPUT>;# Read file into an array close(INPUT);
> 
> #open file to put the lines in
> open(OUTPUT, ">new_dummy_file.txt")|| die "Error
> opening file new_dummy_file.txt. $!\n";
> # Print the array in the new file
> print OUTPUT "@input_lines";

Here is your problem. Remove the quotes.

When an array is interpolated into a string it is effectively the same
as 'join($", @array)', where the special variable $" contains a space by
default. See "perldoc perlop" for more details.

> close(OUTPUT);
> exit(0);

HTH

-- 
Brian Raven


-----------------------------------------------------------------------
The information contained in this e-mail is confidential and solely 
for the intended addressee(s). Unauthorised reproduction, disclosure, 
modification, and/or distribution of this email may be unlawful. If you 
have received this email in error, please notify the sender immediately 
and delete it from your system. The views expressed in this message 
do not necessarily reflect those of LIFFE Holdings Plc or any of its subsidiary 
companies.
-----------------------------------------------------------------------


_______________________________________________
ActivePerl mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to