Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9022307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:28:29+00:00 2026-06-16T05:28:29+00:00

I wrote a script to parse an input text file and output three new

  • 0

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;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T05:28:32+00:00Added an answer on June 16, 2026 at 5:28 am

    Your problem in using \t at the end of the array element. Use function

    join( "\t", ...) 
    

    instead.

    I mean, just create output array, and then use

    print join "\t", @output;
    

    There is untested example:

    #!/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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a script parsing a .csv file in groovy using tokenize, which ended
I wrote this script which counts occurrences of particular pattern in a given file.
Currently, I have a script which does the following. If I have text file
Hi Need a shell script to parse through the csv file - Line by
I wrote a function which runs a php script and returns its output. I
I recently wrote a script which parsed a text representation of a single binary
I wrote a script, where i slurp in UTF-8 encoded HTML-file and then parse
I wrote an automation script to parse an XML file on local, pull some
I'm trying to write a Perl script that will parse the output of the
I wrote some script in a site. The script makes a new spreadsheet and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.