我正在尝试比较两个大文件,并将包含相同内容的每一行写入“Mus musculus”到一个新文件中。我的代码是:
#!/usr/bin/perl
use warnings;
use strict;
my $infile1 = "geneIDs3_MouseToUniProtAccessions.txt";
my $inFH1;
unless (open ($inFH1, "<", $infile1)){
die join (' ', "can't open", $infile1, "for reading", $!);
}
my @list1 = <$inFH1>;
shift @list1;
close $inFH1;
my @list1_new;
for ($a = 0; $a < scalar @list1; $a++){
if ($list1[$a] =~ /(.*Mus musculus.*)/){
push @list1_new, $1;
}
}
my $infile2 = "affymetrixIDs_MouseToUniProtAccessions.txt";
my $inFH2;
unless (open ($inFH2, "<", $infile2)){
die join (' ', "can't open", $infile2, "for reading", $!);
}
my @list2 = <$inFH2>;
shift @list2;
close $inFH2;
my @list2_new;
for ($a = 0; $a < scalar @list2; $a++){
if ($list2[$a] =~ /(.*Mus musculus.*)/){
push @list2_new, $1;
}
}
my @list = ("");
for ($a = 0; $a < scalar @list1_new; $a++){
for ($b = 0; $b < scalar @list2_new; $b++){
if ($list1_new[$a] eq $list2_new[$b]){
push @list, $list1_new[$a];
}
}
}
unless (open (@list, ">", "match_1.txt")){
die join (' ', "can't write the common interest");
}
运行后,perl会给我一个错误Can't use string ("1") as a symbol ref while "strict refs" in use at match_for_part_III_9.pl line 47.
有谁知道如何修复它?任何建议将不胜感激。
答案 0 :(得分:3)
您正在尝试使用@list
的标量表示作为文件句柄来打开match_1.txt
以便在第47行中进行书写。
# VVVV unless (open (@list, ">", "match_1.txt")){ die join (' ', "can't write the common interest"); }
相反,您要创建一个新的文件句柄,并print
@list
该句柄。
open my $fh, '>', 'match_1.txt' or die "Can't write the common interest: $!";
print $fh @list; # will join on $\ implicitly
close $fh;