Hi,


I have a code (below) that reads from a file and output the file content a fter inserting a tab and & in-between different elements into another file.

my $REPORT_FILE  = 'report.txt';

$allRfile = 'AllidentifiedMetabs1.txt';
open(ALL,"<AllidentifiedMetabs1.txt") || die "can't open $allRfile $!";


my $line = <ALL>;
($ID,$name,$M2, $M3, $M4, $M5) = split /\t/, $line;

 @all = ($ID,$name,$M2, $M3, $M4, $M5);
 #chomp @all;


close ALL;


open(OUT, "+>report.txt") or die "Can't open data";

foreach $a(@all) {
	print "$ID & $name & $M2 & $M3 & $M4 & $M5";
};

Input example- a txt file:

1 Sugar 3.13E-01 4.38E-01 4.38E-01 2.19E-01
2 Histidine 6.25E-02 1.88E-01 6.25E-02 6.25E-02


Output required:

1 & Sugar & 3.13E-01 & 4.38E-01 & 4.38E-01 & 2.19E-01
2 & Histidine & 6.25E-02 & 1.88E-01 & 6.25E-02 & 6.25E-02


I would be happy if someone helps.


Thanks

Hello perly!

It looks like you're close! Since you didn't post what you perceive as the problem is with your script, I'm just guessing as to the solution here :) Why not do it all line-by-line though? Something like this might work:

my $REPORT_FILE  = 'report.txt';
my $allRfile = 'AllidentifiedMetabs1.txt';

open(ALL,"$allRfile") || die "can't open $allRfile $!";
open(OUT, "+>$REPORT_FILE") || die "can't open $REPORT_FILE $!"; 

my @lines = <ALL>;
foreach my $line (@lines) {
   ($ID,$name,$M2, $M3, $M4, $M5) = split /\t/, $line;
   print OUT "$ID & $name & $M2 & $M3 & $M4 & $M5";

##   @all = ($ID,$name,$M2, $M3, $M4, $M5);
##   #chomp @all;
} 
 
close ALL;
close OUT;

## open(OUT, "+>report.txt") or die "Can't open data";
 
## foreach $a(@all) {
## 	print "$ID & $name & $M2 & $M3 & $M4 & $M5";
## };

I hope this helps!

Hi Gromit,

Thank you very much for your help!

It works beautifully now.

Best wishes,

Perly

Hello perly!

It looks like you're close! Since you didn't post what you perceive as the problem is with your script, I'm just guessing as to the solution here :) Why not do it all line-by-line though? Something like this might work:

my $REPORT_FILE  = 'report.txt';
my $allRfile = 'AllidentifiedMetabs1.txt';

open(ALL,"$allRfile") || die "can't open $allRfile $!";
open(OUT, "+>$REPORT_FILE") || die "can't open $REPORT_FILE $!"; 

my @lines = <ALL>;
foreach my $line (@lines) {
   ($ID,$name,$M2, $M3, $M4, $M5) = split /\t/, $line;
   print OUT "$ID & $name & $M2 & $M3 & $M4 & $M5";

##   @all = ($ID,$name,$M2, $M3, $M4, $M5);
##   #chomp @all;
} 
 
close ALL;
close OUT;

## open(OUT, "+>report.txt") or die "Can't open data";
 
## foreach $a(@all) {
## 	print "$ID & $name & $M2 & $M3 & $M4 & $M5";
## };

I hope this helps!