[Bioperl-l] foreach (@array)?

Jonathan M. Manning jmanning at genome.wi.mit.edu
Wed Sep 10 14:13:03 EDT 2003


The other replies suggested assigning the foreach. While it's probably a 
good idea, you don't *have* to assign it to a variable. What you have 
should work.

The real problem here is the next_seq() call.

This function consumes your $DB Bio::SeqIO object. Once you get a 
next_seq, you can never go back. I think you're trying to iterate over 
it multiple times.

Here's a quick solution:

my %sequences;
## This goes through your sequences once, and stores them for later use.
while(my $RNA=$DB->next_seq()) {
    ## Added bonus of indexing by display_id.
    ## this allows for quick lookup later
    my $RNAid = $RNA->display_id;
    $sequences{$RNAid} = $RNA;
}

## Now just use:
foreach $id (@mirna) {
    next unless (exists $sequences{$id} && defined $sequences{$id});
    $seqRNA = $sequences{$id}->seq();

    # rest of your code here...
     for (my $i=0; $i<length($geneRNA); $i++) {
       $subgene=substr($geneRNA,$i,length($seqRNA));
       $percentage = align_subs($seqRNA, $subgene);

        if($percent<$percentage) {
         push (@RNAname,$id);
         push (@subgene, $subgene);
         push (@percentage,$percentage);
         push (@position,($i+1));
        }
     }

}

Hope this helps,
~Jonathan


Vesko Baev wrote:
> Hi,
> When I start my script, it return results only for first variable in @array, but in the raw 'foreach (@array){' in the @array there is two or more variables, but anytime it returns the result whit first variable:
> 
> foreach (@mirna) { 
>      $id=$_; 
>   while (my $RNA=$DB->next_seq()) { 
>      $RNAid=$RNA->display_id;
>      if ($RNAid eq $id) {
>      $seqRNA=$RNA->seq();
>      }
>      else {next};
> 
> 
>    for (my $i=0; $i<length($geneRNA); $i++) {
>      $subgene=substr($geneRNA,$i,length($seqRNA));
>      $percentage = align_subs($seqRNA, $subgene);
> 
>       if($percent<$percentage) {
>        push (@RNAname,$id);
>        push (@subgene, $subgene);
>        push (@percentage,$percentage);
>        push (@position,($i+1));
>       }
>    }
>   };
>  };
> };
> Thanks! 
> 
> 
> -----------------------------------------------------------------
> http://gsm.ABV.bg - Вземи сега мелодия за твоя телефон !
> _______________________________________________
> Bioperl-l mailing list
> Bioperl-l at portal.open-bio.org
> http://portal.open-bio.org/mailman/listinfo/bioperl-l


-- 
Jonathan Manning <jmanning at genome.wi.mit.edu>
Whitehead Institute Center for Genome Research
Finishing Process Analyst / Data Analyst



More information about the Bioperl-l mailing list