I wrote a script to parse an input text file and output three new text files. The input file has a header line, which is left out of each of the three output files. Each line of the output files gets the first three entries of every input file line, then gets only a portion of the entries in the input line. All files are tab delimited.
My problem is that I keep getting an extra white-space at the end of my output lines. I can see this white-space at the end of the lines of my output files when viewing in VIM. I can’t see these extra white-spaces when viewing in excel.
Previously, using an earlier version of my script, I could see extra white-space at the end of my lines using excel, but only by using the “select all” feature. The data displayed in excel was the result of my perl script printing an array, where I coded print $outfile "@array". I learned elsewhere on stackoverflow that typing print "@array" causes a kind of interpolation that adds in white-spaces to my lines. White-spaces resulting from this interpolation were not immediately visible in excel, but could be seen when “select all” was used to highlight the data. So, I changed my code from print $outfile "@array" to print $outfile @array. This change results in output files that still maintain a white-space at the end of each output line, a white-space visible in VIM but not in excel. This is where I am stuck.
Here is my script below. The first part just opens and reads the input file. Then some arrays are declared. Next, a for loop is begun to begin parsing the input file line by line, and pushing appropriate content into appropriate arrays. A substitution is implemented for one array. Again, all files are tab delimited. Finally, the arrays are printed to three outfiles, using the phrasing print $outfile @array and not print $outfile "@array".
Do you see the problem? Thanks!
#!/usr/bin/perl
use strict; use warnings;
die "usage: [ imputed genotype.file ]\n" unless @ARGV == 1;
my $imputed = $ARGV[ 0 ];
open ( my $FILE, "<$imputed" );
my @data = <$FILE>;
my @ADD = ();
my @DOM = ();
my @IMP = ();
for ( my $i = 1; $i < scalar @data; $i++ ) ### for each line data[i], and use $i = 1 to
### skip header, 0 to include it output
{
my $line = $data[ $i ];
chomp $line;
my @entries = split( '\t', $data[ $i ] );
push( @ADD, "$entries[ 0 ]\t$entries[ 1 ]\t$entries[ 2 ]\t" );
push( @DOM, "$entries[ 0 ]\t$entries[ 1 ]\t$entries[ 2 ]\t" );
push( @IMP, "$entries[ 0 ]\t$entries[ 1 ]\t$entries[ 2 ]\t" );
for ( my $i = 3; $i < scalar @entries - 1 ; $i+=3 ) ### for each entry per line
{
push( @ADD, "$entries[ $i ]\t" );
push( @DOM, "$entries[ $i + 1 ]\t" );
if ( $entries[ $i + 2 ] eq 'NA' ) ### to replace any occuring "NA"s with blanks
{
$entries[ $i + 2 ] =~ s/NA//;
}
push( @IMP, "$entries[ $i + 2 ]\t" );
}
push( @ADD, "\n" );
push( @DOM, "\n" );
push( @IMP, "\n" );
} ### for loop
open my $Afile, ">$imputed" . "_ADD.txt" or die $!;
print $Afile @ADD;
close $Afile;
open my $Dfile, ">$imputed" . "_DOM.txt" or die $!;
print $Dfile @DOM;
close $Dfile;
open my $Ifile, ">$imputed" . "_IMP.txt" or die $!;
print $Ifile @IMP;
close $Ifile;
Your problem in using \t at the end of the array element. Use function
instead.
I mean, just create output array, and then use
There is untested example: