Reputation: 161
I have a file of around 3000 lines. I am matching every word in the document against reference lists that i have. If the word matches to the one in my list then I substitute it. The problem now is that the code only prints the last line, but not the entire file.
I am sure that my code is not that efficient and will take a long time to process, is there anyway to increase the efficiency of the code
open IN, "drug_list.txt" or die "No such file:$!\n";
open OUT, ">synergy1.txt" or die;
while(<IN>) {
my @a=split /\t/,$_;
$a[0]=~s/\s//g;
$a[1]=~s/\s//g;
$a[2]=~s/\s//g;
$b[$i]=$a[0];
$c[$i]=$a[1];
$d[$i]=$a[2];
++$i;
}
use Data::Dumper;
open FILE, "input.txt" or die "No such file:$!\n";
while(<FILE>) {
my $line= $_;
chomp $line;
$line =~ s/(\)|\()//g;
$line =~ s/,/ ,/g;
$line =~ s/\./ ./g;
@array = split ' ',$line;
for($k=0;$k<$i;++$k) {
foreach $n(@array) {
if($n=~m/^\Q$b[$k]\E$/i) {
$n=~s/$n/<span style="background-color:yellow;">$n<\/span>/;
}
if($n=~m/^\Q$c[$k]\E$/i) {
$n=~s/$n/<span style="background-color:red;">$n<\/span>/;
}
if($n=~m/^\Q$d[$k]\E$/i) {
$n=~s/$n/<span style="background-color:blue;">$n<\/span>/;
}
} # end foreach
} # end for
} # end while
print OUT "@array";
close(FILE);
close(IN);
Upvotes: 0
Views: 1278
Reputation: 11536
I have edited the code, now only the last line get printed
Now you are re-assigning to @array
at each pass:
while(<FILE>)
{
[...]
@array = split ' ',$line;
[...]
}
print OUT "@array";
When array gets printed out, it only contains the last line. The easiest solution is probably to move the print OUT
line to inside the while()
loop, right at the end. This way it will get printed out before it gets reassigned the contents of a new line.
Upvotes: 3
Reputation: 2791
Also you close(IN)
and a few lines later you try to do while (<IN>)
.
Please provide working code and example data. Otherwise it will be a tough job figuring out what the problem is.
Upvotes: 1
Reputation: 385655
You create two variables named @array
.
my @array;
while(<IN>)
{
...
my @array = split ' ',$line;
Upvotes: 2