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;

Recommended Answers

All 2 Replies

I see what your problem is... but the advice I'm going to give is going to be more useful in the long run than telling you straight.

# after 'use strict'
use Data::Dumper;

# then in your code, when you want to see the contents of %seqs
warn Dumper(\%seqs);

Put the warn in your first while loop so you can see the hash being built.

Dumper is something you should know about, it's invaluable for debugging stuff like this.

I see what your problem is... but the advice I'm going to give is going to be more useful in the long run than telling you straight.

# after 'use strict'
use Data::Dumper;

# then in your code, when you want to see the contents of %seqs
warn Dumper(\%seqs);

Put the warn in your first while loop so you can see the hash being built.

Dumper is something you should know about, it's invaluable for debugging stuff like this.

Ah. That will make my life so much easier! ...didn't think i'd get the first loop wrong since I've written that syntax more than a few times. Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.