Search code examples
csvperlparsingtext-files

where do the trailing commas come from (perl)


Here is a perl script that takes a tab delimited output file and outputs three different text files, also tab delimited. Another user on SO helped me correct a mistake that created extra white-space at the end of each line in the output files. However, I wish to instead to output comma delimited text. When I substitute print $Afile join( ",", @ADD) , "\n"; instead of print $Afile join( "\t", @ADD) , "\n"; I get two trailing commas at the end of each line in the output files. Where are these coming from?

#!/usr/bin/perl
use strict; use warnings;

die "usage: [ imputed genotype.file ]\n" unless @ARGV == 1;

open my $Afile, ">$imputed" . "_ADD.txt" or die $!;
open my $Dfile, ">$imputed" . "_DOM.txt" or die $!;
open my $Ifile, ">$imputed" . "_IMP.txt" or die $!;

<>; #skip header
while(<>){ 
  chomp;
  my @entries = split( '\t', $_ );

  my @ADD = ();
  my @DOM = ();
  my @IMP = ();

  push( @ADD, $entries[ 0 ], $entries[ 1 ], $entries[ 2 ]);
  push( @DOM, $entries[ 0 ], $entries[ 1 ], $entries[ 2 ]);
  push( @IMP, $entries[ 0 ], $entries[ 1 ], $entries[ 2 ]);

  for ( my $i = 3; $i < scalar @entries - 1 ; $i+=3 ) { ### for each entry per line
      push( @ADD, $entries[ $i ] );
      push( @DOM, $entries[ $i + 1 ] );

  $entries[ $i + 2 ] =~ s/^NA$//; 

      push( @IMP, $entries[ $i + 2 ] );
  }

  print $Afile join( "\t", @ADD) , "\n"; 
  print $Dfile join( "\t", @DOM) , "\n"; 
  print $Ifile join( "\t", @IMP) , "\n"; 

} ### for loop   

close $Afile;
close $Dfile;
close $Ifile;

Solution

  • Since tabs are white space characters you do not see them with your current version but you already have trailing tabs. They are due to null elements in your arrays. You can filter them with grep though:

    print $Afile join( ",", grep { $_ } @ADD) , "\n";