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

hello Monks
How can replicate multiple files in one file?, I read previous post I got answers, but my problem is that is adding a blank space before each line after the firt one of each file?
#here my code foreach my $file (@filenames) { open (INFILE, "$file") || die ("Can't open file $file$!"); @all=<INFILE>; close (INFILE); print OUTFILE1 "@all"; } close (OUTFILE);
I dont' want blank line separating each file (which is doing ok), but I also don't want blank lines at the end of the file
Thanks

Replies are listed 'Best First'.
Re: Replicate file
by kvale (Monsignor) on Apr 13, 2004 at 22:34 UTC
    You are getting the spaces because you are stringifying the array. Try
    foreach my $file (@filenames) { open (INFILE, "$file") || die ("Can't open file $file$!"); @all=<INFILE>; close (INFILE); print OUTFILE1 @all; }
    or better yet
    foreach my $file (@filenames) { open (INFILE, "$file") || die ("Can't open file $file$!"); print OUTFILE1 <INFILE>; close (INFILE); }

    -Mark

      Thank you very much!! It works, the only think it is leaving a blank line at the end of the file
Re: Replicate file (through concatenation)
by grinder (Bishop) on Apr 14, 2004 at 06:03 UTC

    Rather than slurping in the entire file before writing it out, it might be better to read in blocks of 4Kb (or some amount suited to your OS):

    $/ = \ 4096; # read in 4Kb blocks foreach my $file (@filenames) { open (INFILE, "$file") || die ("Can't open file $file$!"); while( <INFILE> ) { print OUTFILE1; } }
    And if you don't really care about files that don't exist (i.e. just ignore them) you can simplify, simplify, albeit by introducing a trick with @ARGV magic:
    { local @ARGV = @filenames; while( <> ) { print OUTFILE1; } }

    <pendantry> ... and the correct term is concatenating (although some argue that it is catenating), rather than replicating. </pendantry>

Re: Replicate file
by duff (Parson) on Apr 14, 2004 at 04:21 UTC

    Are you sure you want to exit your program if one of the files can't be opened? I might have written it something like this:

    for my $file (@filenames) { open IN,$file or warn("unable to open $file - $!\n"), next; print OUT <IN>; close IN; }

    You'll also notice that I didn't quote the filename. No need for that