jsleonard has asked for the wisdom of the Perl Monks concerning the following question:

#!/usr/bin/perl #author: j. s. leonard 2006 print header(); use CGI ':standard'; # Why are spaces added to array lines when your file # contains linefeeds? Here's an example. # # Note that testout4.txt is correct, but all others # have a space added after the linefeed. # # Also note that testout3.txt is correct, because it # does not contain linefeeds. # # Thanks. open(*FH, "<testin1.txt"); #open file my @Data=<FH>; #read file into array close (*FH); #close file open(*FH, ">testout1.txt"); #open file print FH "@Data"; #read file into array close (*FH); #close file open(*FH, "<testin2.txt"); #open file my @Data=<FH>; #read file into array close (*FH); #close file open(*FH, ">testout2.txt"); #open file print FH "@Data"; #read file into array close (*FH); #close file open(*FH, "<testin3.txt"); #open file my @Data=<FH>; #read file into array close (*FH); #close file open(*FH, ">testout3.txt"); #open file print FH "@Data"; #read file into array close (*FH); #close file open(*FH, "<testin1.txt"); #open file my @Data=<FH>; #read file into array close (*FH); #close file open(*FH, ">testout4.txt"); #open file foreach (@Data) { #loop through array print FH "$_"; #print to file } close (*FH); #close file exit; #========================== # ALREADY TRIED: #========================== # # * Windows, Linux doesn't matter. # * Tested on many different machines. # * # * # * # * # * #========================== # DEPENDENCIES: #========================== # testin1.txt (Normal) #---------------------------- #line1 #line2 #line3 #line4 # testin1.txt HEX (CRt-Lf) #---------------------------- # 6C 69 6E 65 31 0D 0A # 6C 69 6E 65 32 0D 0A # 6C 69 6E 65 33 0D 0A # 6C 69 6E 65 34 # testin2.txt HEX (Lf) #---------------------------- # 6C 69 6E 65 31 0A # 6C 69 6E 65 32 0A # 6C 69 6E 65 33 0A # 6C 69 6E 65 34 # testin3.txt HEX (CRt) #---------------------------- # 6C 69 6E 65 31 0D # 6C 69 6E 65 32 0D # 6C 69 6E 65 33 0D # 6C 69 6E 65 34 #========================== # OUTPUT: #========================== # testout1.txt #---------------------------- #line1 # line2 # line3 # line4 # testout1.txt HEX #---------------------------- # 6C 69 6E 65 31 0D 0A # 20 6C 69 6E 65 32 0D 0A # 20 6C 69 6E 65 33 0D 0A # 20 6C 69 6E 65 34 # testout2.txt HEX #---------------------------- # 6C 69 6E 65 31 0D 0A # 20 6C 69 6E 65 32 0D 0A # 20 6C 69 6E 65 33 0D 0A # 20 6C 69 6E 65 34 # testout3.txt HEX #---------------------------- # 6C 69 6E 65 31 0D # 6C 69 6E 65 32 0D # 6C 69 6E 65 33 0D # 6C 69 6E 65 34 # testout4.txt HEX #---------------------------- # 6C 69 6E 65 31 0D 0A # 6C 69 6E 65 32 0D 0A # 6C 69 6E 65 33 0D 0A # 6C 69 6E 65 34

Replies are listed 'Best First'.
Re: spaces after linefeeds when printing arrays
by wfsp (Abbot) on May 14, 2006 at 10:46 UTC
Re: spaces after linefeeds when printing arrays
by Zaxo (Archbishop) on May 14, 2006 at 12:20 UTC

    When an array in interpolated in quotes, the value of the $" global variable is used as a seperator. By default, $" is a single space.

    To prevent, either don't quote the array of lines, or else set local $" = ''; in a short scope before printing.

    After Compline,
    Zaxo

Re: spaces after linefeeds when printing arrays
by davorg (Chancellor) on May 15, 2006 at 08:37 UTC

    Sounds like another good reason for not quoting variables unnecessarily.

    If you leave off the quotes in all of your "print" statements, then it will look how you want it.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg