1

I have two files (commented out in code below), and I would like to print out field 4 from file 2 when field 2 from file 1 falls numerically between fields 2 and 3 from file2, given field 1 from both files match each other.

#! perl -w  
use strict; 
use warnings;

#File1
#Chromosome Position ID A1 A2 Highalt_alt_counts Lowalt_alt_counts     Highalt_missing Lowalt_missing
#1 10492 rs55998931 C T 6 7 3 3
#1 10583 rs58108140 G A 2 4 6 4
#1 10665 . C G 1 2 11 9

#File2
#chrom txStart txEnd name2
#1 66999638 67216822 SGIP1
#1 66999251 67216822 SGIP1
#1 33547778 33586132 AZIN2
#1 8378144 8404227 SLC45A1

my @loci;
open( my $loci_in, "<", "Highalt.Lowalt.allelecounts" ) or die $!;
while (<$loci_in>) {
    my ( $chr, $bp ) = split;
    next if m/CHR/;
    push @loci, [ $chr, $bp ];
}
close $loci_in;

my $filename = shift @ARGV;
open( my $input, "<", $filename ) or die $!;
while (<$input>) {
    next if m/bin/;
    my ( $chr, $start, $end, $gene ) = split;
    foreach my $locus (@loci) {
        if (    $chr == $locus[0]
                and $start <= $locus[1]
        and $end >= $locus[1] )
        {
            print "$locus[0] $locus[1] $gene\n";

        }
    }
}
close($input);

I am getting the warning:

Global symbol "@locus" requires explicit package name at find_gene_2.pl line 29.

Can someone point out the error?

Matthew Lock
  • 13,144
  • 12
  • 92
  • 130
theo4786
  • 159
  • 1
  • 15

2 Answers2

2

When you try to access $locus[0], this refers to the first element of the array @locus. In your case, the $locus you mean to access is an array reference, and its first element can be dereferenced with an arrow:

foreach my $locus (@loci) {            # @loci is array of array refs
    if (    $chr == $locus->[0]
            and $start <= $locus->[1]
    and $end >= $locus->[1] )
    {
        print "$locus->[0] $locus->[1] $gene\n";

    }
}
pilcrow
  • 56,591
  • 13
  • 94
  • 135
Jim Davis
  • 5,241
  • 1
  • 26
  • 22
1

locus is a scalar not an array, but you are attempting to use it as if it's an array when you say $locus[0]. You probably meant to get the characters out of $locus right? If so you should use substr to access the individual characters of a scalar string.

If you are new to perl then the characters at the beginning of the variable names can be confusing. See here for a good explanation Why do Perl variables need to start with $, %,@ (sigils)?

Community
  • 1
  • 1
Matthew Lock
  • 13,144
  • 12
  • 92
  • 130