in reply to Question on loops

Each iteration of your while loop overwrites the variables $V1 and $V2, so by the time your while loop is complete, only the final line's values are there.

Something like this (untested) should work:

use Bio::SeqIO; my (%all_V1, %all_V2); # hashes to store $V1 and $V2 open (LIST1, "list"); while (<LIST1>) { ($V1, $V2) = split /\t/; $all_V1{$V1}++; # FIX: this line and the next were originally $all_V2{$V2}++; # incorrectly prefixed with "push". } close(LIST1); my $seqIO_object = Bio::SeqIO->new(-file=>"infile.gb"); my $seq_object = $seqio_object ->next_seq; for my $feat_object ($seq_object->get_SeqFeatures){ if ($feat_object->primary_tag eq "CDS"){ if ($feat_object->has_tag('locus_tag')){ for my $V3 ($feat_object ->get_tag_values('locus_tag')){ if (exists $all_V1{$V3}){ print "locus_tag: ", $V3, " is unique\n"; } } } } }

By the way, $V1, $V2 and $V3 are very badly named variables. If you want to be able to come back to your code in a few months and fix a bug, it really helps if your variables have useful names.

Replies are listed 'Best First'.
Re^2: Question on loops
by Bforde (Initiate) on Dec 12, 2011 at 15:04 UTC

    Hello tobyink

    Thank you for the quick response. I figured that V1 and V2 were being over written had started to look at hashes. I understand what they are doing but not how to use them correctly

    the code does have an error

    Type of arg 1 to push must be array (not postincrement (++)) at test.pl line 10, near "++;"

    Type of arg 1 to push must be array (not postincrement (++)) at test.pl line 10, near "++;"

    must the V1 and V2 variables be changed to arrays before being added to the hash

    Brian

    I can assure you that my actual variable have appropriate names

      Meh. Remove the two pushes. Should just be:

      while (<LIST1>) { ($V1, $V2) = split /\t/; $all_V1{$V1}++; $all_V2{$V2}++; }

      ... the pushes were left overs from an earlier response I typed up but didn't submit, which used arrays.

      I'll correct the code in my original answer too.