PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, August 29, 2022

[FIXED] where do the trailing commas come from (perl)

 August 29, 2022     csv, parsing, perl, text-files     No comments   

Issue

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"; 


Answered By - perreal
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing