I'm just learning PERL. I'm working on a short practice data set. All I want to do is to see if A) all values from a list exist in a hash i've made from a separate data file, and B) are the values that do exist defined--i.e. have a value associated with them.
I know that when I simply change the function "defined" for "exists" I get the correct output that WD9999 and WD0101 do not existing in the data file. When I use "defined" I get the following output:
WD0001 does not exist in seqs26
WD0003 does not exist in seqs26
WD0101 does not exist in seqs26
WD0004 does not exist in seqs26
WD9999 does not exist in seqs26
This suggests that there is no value associated with any key in my hash. Quirk of PERL i'm not getting?
This is my list (list.txt):
WD0001
WD0003
WD0101
WD0004
WD9999
and this is the data file i'm checking against (seqs2.26.txt):
WD0001 AATCCTATCGCTTAAAATATATA
WD0002 TCAAGTCGATTATATTTATTTCT
WD0003
WD0004 CTAGCTAGCTAGCTAGCTAGCTA
My code:
use warnings;
use strict;
open(SEQS, "seqs2.26.txt");
my %seqs;
#loop loads data from seqs2.26.txt into %seqs
while (my $line = <SEQS>)
{
chomp $line;
my @array= split(/\s/, $line);
$seqs{$array[0]} = $seqs{$array[0]};
}
close SEQS;
open(LIST, "list.txt") or die;
#supposed to check to see if key from list is defined in %seqs
while (my $gene_list = <LIST>)
{
chomp $gene_list;
my @list_array = split(/\s/, $gene_list);
unless (defined $seqs{$list_array[0]})
{
print "$list_array[0] does not exist in seqs26\n";
}
}
close LIST;
exit;