1

I have two tables. First one is $sampleand looks like this:

col1 col2
A    1
A    3
A    4
B    7
...  ...

Second one is $exonand looks like this:

col1     col2  col3 col4   col5
name1    A     1    100    200
name2    A     2    300    400
name3    A     3    500    600
name4    A     4    700    800

I want to check if there is a match between col1 and col2 from $sampleand col2 and col3from exon.

I normally use hashes for this in Perl. I know how it works when you are just looking for a match between two columns. But I'm stuck now because values from two columns should match. This is what I have for now

my %hash = ();
while(<$sample>){
    chomp;
    my @cols = split(/\t/);
    my $keyfield = $cols[0]; #col1
    my $keyfield2 = $cols[1]; #col2
    push @{ $hash{$keyfield}}, $keyfield2}; #this is probably not correct
} 
    seek $exon,0,0; #cursor resetting

 while(<$exon>){
    chomp;
    my @cols = split(/\t/);
    my $keyfield = $cols[1]; #col2
    my $keyfield2 = $cols[2]; #col3
    if (exists($hash{$keyfield}) && exists($hash{$keyfield2})) {
        print $output $cols[0], "\t", $cols[3], "\t", $cols[4], "\n";
 }
 }

2 Answers 2

3

You should use a concatenation of col2 and col3 values as the keys for your hastable

my %hash = ();
while(<$sample>){
    chomp;
    my @cols = split(/\t/);
    my $keyfield = $cols[0] #col1
    my $keyfield2 = $cols[1] #col2
    my $key = "$keyfield - $keyfield2";
    $hash{$key}=1;
} 
    seek $exon,0,0 #cursor resetting

 while(<$exon>){
    chomp;
    my @cols = split(/\t/);
    my $keyfield = $cols[1]; #col2
    my $keyfield2 = $cols[2]; #col3
    my $key = "$keyfield - $keyfield2";
    if (exists($hash{$key}) {
        print $output $cols[0], "\t", $cols[3], "\t", $cols[4], "\n";
     }
 } 
Sign up to request clarification or add additional context in comments.

Comments

1

You can put both fields as key separarted with a delimiter in your hash:

my @cols = split(/\t);
my $keyfield = $cols[0]."--".$cols[1];
push @{ $hash{$keyfield}}, value}; 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.