Brian
Brian

Reputation: 4243

Perl, read from two files and write to a third

I am having trouble getting my program to work. Basically, I read in from two files, then write the data into one. Can anyone point out what the problem is? I keep getting errors about instantiation on the print OUT statements where I am trying to write to the file. Here is my code:

print "Please input file name \n";
$infile=<DATA>;
$infile2=<DATA>;

open IN, "$infile";
open JUNK, "$infile2";
open OUT, '>' ,'convertedback.txt';

$line = <IN>;
$line2 = <JUNK>;

#pull first line from sample (name 1)
print OUT "$line";
$line =<IN>;
#pull sequence line from FASTQ
print OUT "$line";
#pull line from quality file *2
print OUT "$line2";
$line2 =<JUNK>;
print OUT "$line2";

#Repeat until EOF
while($line =<IN>) {#for lines 5 to end

#Build Line 1
print "line 1 inf (name) is\n";
print $line2;
print OUT "$line2";


#Build Line 2
print "line 2 inf (seq) is\n";
print $line;
print OUT "$line";

#Build Line 3
$line2 =<JUNK>;
print "line 3 inf (quality) is\n";
print $line2;
print OUT "$line2";

#Build Line 4
$line2 =<JUNK>;
print "line 3 inf (quality) is\n";
print $line2;
print OUT "$line2";

}#while $line=<IN>
close (IN);
close (OUT);

print "Done!\n";

__DATA__
outfilenew.txt
sample.qualities

Upvotes: 0

Views: 5899

Answers (1)

Nikhil Jain
Nikhil Jain

Reputation: 8332

use strict and use warnings in the beginning of the script.

use three argument open, like

open(my $fh, '<', "input.txt") or die $!;

use while loop to read lines from files like,

while(my $line = <>){
   #do something
}

script looks like,

use strict;
use warnings;
print "Please input file name \n";
my $infile=<DATA>;
my $infile2=<DATA>;

open(my $in,'<', "$infile") or die $!;
open(my $junk,'<',"$infile2") or die $!;
open(my $out, '>' ,'convertedback.txt') or die $!;

my $line = <$in>;
my $line2 = <$junk>;

#pull first line from sample (name 1)
print $out "$line";
$line =<$in>;
#pull sequence line from FASTQ
print $out "$line";
#pull line from quality file *2
print $out "$line2";
$line2 =<$junk>;
print $out "$line2";

#Repeat until EOF
while($line =<$in>) {#for lines 5 to end

#Build Line 1
print "line 1 inf (name) is\n";
print $line2;
print $out "$line2";


#Build Line 2
print "line 2 inf (seq) is\n";
print $line;
print $out "$line";

#Build Line 3
$line2 =<$junk>;
print "line 3 inf (quality) is\n";
print $line2;
print $out "$line2";

#Build Line 4
$line2 =<$junk>;
print "line 3 inf (quality) is\n";
print $line2;
print $out "$line2";

}#while $line=<IN>
close ($in);
close ($out);

print "Done!\n";

__DATA__
outfilenew.txt
sample.qualities

Upvotes: 6

Related Questions