Daisie Huang | 14 Feb 06:21 2013
Picon

Re: Question regarding while loops for reading files

I think you need to reset the pointer to the filehandle before you go 
through the while loop the second time: seek $fh,0,0

On Wednesday, February 13, 2013 6:46:41 PM UTC-8, Tiago Hori wrote:
>
> Hey Guys,
>
> I am still at the same place. I am writing these little pieces of code to 
> try to learn the language better, so any advice would be useful. I am again 
> parsing through tab delimited files and now trying to find fish from on id 
> (in these case families AS5 and AS9), retrieve the weights and average 
> them. When I started I did it for one family and it worked (instead of the 
>  <at> families I had a scalar $family set to AS5). But really it is more useful 
> to look at more than one family at time (I should mention that are 2 types 
> of fish per family one ends in PS , the other doesn't). So I tried to use a 
> foreach loop to go through the file twice, once with a the search value set 
> to AS5 and a second time to AS9. It works for AS5, but for some reason, the 
> foreach loop sets $test to AS9 the second time, but it doesn't go through 
> the while loop. What am I doing wrong? 
>
> here is the code:
>
> #! /usr/bin/perl
> use strict;
> use warnings;
>
> my $file = $ARGV[0];
> my  <at> family = ('AS5','AS9');
> my $i;
> my $ii;
> my $test;
>
> open (my $fh, "<", $file) or die ("Can't open $file: $!");
>
> foreach ( <at> family){
>     $test = $_;
>     my  <at> data_weight_2N = ();
>     my  <at> data_weight_3N = ();
>     while (<$fh>){
>         chomp;  
>         my $line = $_;
>         my  <at> data  = split ("\t", $line);
>         if ($data[0] !~ /[0-9]*/){
>         next;}
>         elsif ($data[1] eq "ABF09-$test"){
>             $i += 1; 
>             push ( <at> data_weight_2N,  $data[6]);
>         }elsif ($data[1] eq "ABF09-".$test."PS"){
>         $ii += 1;
>             push ( <at> data_weight_3N,$data[6]);
>     }
> }
>     my $mean_2N = &average (\ <at> data_weight_2N);
>     my $stdev_2N = &stdev (\ <at> data_weight_2N);
>     my $stderr_2N = ($stdev_2N/sqrt($i));
>
>     print "These are the the avearge weight, stdev and stderr for $test 
> 2N:\t", $mean_2N,"\t",$stdev_2N,"\t",$stderr_2N, "\n";
>
>     my $mean_3N = &average (\ <at> data_weight_3N);
>     my $stdev_3N = &stdev (\ <at> data_weight_3N);
>     my $stderr_3N = ($stdev_3N/sqrt($i));
>
>     print "These are the the avearge weight, stdev and stderr for $test 
> 3N:\t", $mean_3N,"\t",$stdev_3N,"\t",$stderr_3N, "\n";
> }
>
> close ($fh);
>
> sub average{
>         my($data) =  <at> _;
>         if (not  <at> $data) {
>                 print ("Empty array\n");
>                 return 0;
>         }
>         my $total = 0;
>         foreach ( <at> $data) {
>                 $total += $_;
>         }
>         my $average = $total /  <at> $data;
>         return $average;
> }
>
> sub stdev{
>         my($data) =  <at> _;
>         if( <at> $data == 1){
>                 return 0;
>         }
>         my $average = &average($data);
>         my $sqtotal = 0;
>         foreach( <at> $data) {
>                 $sqtotal += ($average-$_) ** 2;
>         }
>         my $std = ($sqtotal / ( <at> $data-1)) ** 0.5;
>         return $std;
> }
>
> Thanks,
>
> T.
>
> -- 
> "Education is not to be used to promote obscurantism." - Theodonius 
> Dobzhansky.
>
> "Gracias a la vida que me ha dado tanto
> Me ha dado el sonido y el abecedario
> Con él, las palabras que pienso y declaro
> Madre, amigo, hermano
> Y luz alumbrando la ruta del alma del que estoy amando
>
> Gracias a la vida que me ha dado tanto
> Me ha dado la marcha de mis pies cansados
> Con ellos anduve ciudades y charcos
> Playas y desiertos, montañas y llanos
> Y la casa tuya, tu calle y tu patio"
>
> Violeta Parra - Gracias a la Vida
>
> Tiago S. F. Hori. PhD.
> Ocean Science Center-Memorial University of Newfoundland 
>

_______________________________________________
Bioperl-l mailing list
Bioperl-l <at> lists.open-bio.org
http://lists.open-bio.org/mailman/listinfo/bioperl-l

Gmane