Wenjia Zhai
Wenjia Zhai

Reputation: 71

Can't use string as a symbol ref while "strict refs" in use

I'm trying to compare two large files and write every line containing the same content with "Mus musculus" into a new file. My code is:

#!/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");
}

After running, perl gives me an error stated Can't use string ("1") as a symbol ref while "strict refs" in use at match_for_part_III_9.pl line 47.

Does anyone know how to fix it? Any suggestion will be greatly appreciated.

Upvotes: 0

Views: 2062

Answers (1)

simbabque
simbabque

Reputation: 54333

You are trying to use the scalar representation of @list as your filehandle to open match_1.txt for writing in line 47.

#             VVVV
unless (open (@list, ">", "match_1.txt")){
    die join (' ', "can't write the common interest");
}

Instead, you want to create a new file handle, and print your @list to that handle.

open my $fh, '>', 'match_1.txt' or die "Can't write the common interest: $!";
print $fh @list; # will join on $\ implicitly
close $fh;

Upvotes: 3

Related Questions