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

Hi perl saviors! Im trying to print elements from two different arrays to my output file using \t. (i want the two arrays to look like tsv in my output file). but my second element automatically prints in the second line. Using chomp did not help. Please tell me if there is any other way i could use to print the data like tsv.
for ($i=0;$i<(scalar(@np));$i++) { if ($gap[$index]>=-200) { print OFILE "$coordinate[$index]\t$np[$index]\n"; } else { print OFILE "End of block $index.\n"; print OFILE "$coordinate[$index]\n"; print OFILE "Start of block $index.\n"; } $index++; }
my current output looks like this below
4775792 p 4775842 p 4775852 p 4775902 p 4775952 p
rather I want it to look like this
4775792 p 4775842 p 4775852 p 4775902 p 4775952 p 4776002 n
please help me with this. i have trying so hard to figure out this!

Replies are listed 'Best First'.
Re: print two arrays into a tsv format
by hdb (Monsignor) on Jul 03, 2013 at 06:20 UTC

    First you should inspect your array @coordinate whether or not there are newlines in the elements. This can best be done with Data::Dumper:

    use Data::Dumper; ... print Dumper \@coordinate;

    If this confirms that there are newlines, you can either remove them from the elements of @coordinate using chomp @coordinate; or you need to fix it at the time of populating @coordinate. The latter would be preferred but you have not posted that piece of code.

      chomp @coordinate worked! thank you so much.

        You have chosen to cure the symptoms not the disease...

Re: print two arrays into a tsv format
by Anonymous Monk on Jul 03, 2013 at 06:24 UTC