Running a nested while loop inside a foreach loop in Perl -


i'm trying use foreach loop loop through array , use nested while loop loop through each line of text file see if array element matches line of text; if push data line new array perform calculations.

the outer foreach loop appears working correctly (based on printed results each array element) inner while loop not looping (same data pushed array each time).

any advice?

the code below

#! /usr/bin/perl -t  use cgi qw(:cgi-lib :standard);  print "content-type: text/html\n\n";  $input       = param('sequence'); $meanexpfile = "final_expression_complete.txt";  open(file, $meanexpfile) or print "unable open file";  @meanmatches;  @regex = (split /\s/, $input);  foreach $regex (@regex) {      while (my $line = <file>) {         if ( $line =~ m/$regex\s(.+\n)/i ) {             push(@meanmatches, $1);         }     }      $average                  = average(@meanmatches);     $std_dev                  = std_dev($average, @meanmatches);     $average_round            = sprintf("%0.4f", $average);     $stdev_round              = sprintf("%0.4f", $std_dev);     $coefficient_of_variation = $stdev_round / $average_round;     $cv_round                 = sprintf("%0.4f", $coefficient_of_variation);      print font(         { color => "blue" }, "<br><b>$regex average: $average_round    &nbspstandard deviation: $stdev_round&nbspcoefficient of  variation(cv): $cv_round</b>"     ); }  sub average {     (@values) = @_;     $count    = scalar @values;     $total    = 0;      $total += $_ @values;      return $count ? $total / $count : 0; }  sub std_dev {     ($average, @values) = @_;     $count       = scalar @values;     $std_dev_sum = 0;      $std_dev_sum += ($_ - $average)**2 @values;      return $count ? sqrt($std_dev_sum / $count) : 0; } 

yes, advice be:

  • turn on strict , warnings.
  • perltidy code,
  • use 3 argument open: open ( $inputfile, "<", 'final_expression.txt' );
  • die if doesn't open - rest of program irrelevant.
  • chomp $line
  • you iterating filehandle, once you've done you're @ end of file next iteration of foreach loop while loops becomes null operation. simplistically, reading file array my @lines = <file>; fix this.

so in mind:

#!/usr/bin/perl -t  use strict; use warnings;  use cgi qw(:cgi-lib :standard); print "content-type: text/html\n\n";  $input       = param('sequence'); $meanexpfile = "final_expression_complete.txt"; open( $input_file, "<", $meanexpfile ) or die "unable open file"; @meanmatches;  @regex = ( split /\s/, $input ); @lines = <$input_file>; chomp (@lines); close($input_file) or warn $!;  foreach $regex (@regex) {     foreach $line (@lines) {         if ( $line =~ m/$regex\s(.+\n)/i ) {             push( @meanmatches, $1 );         }     }      $average                  = average(@meanmatches);     $std_dev                  = std_dev( $average, @meanmatches );     $average_round            = sprintf( "%0.4f", $average );     $stdev_round              = sprintf( "%0.4f", $std_dev );     $coefficient_of_variation = $stdev_round / $average_round;     $cv_round = sprintf( "%0.4f", $coefficient_of_variation );      print font(         { color => "blue" }, "<br><b>$regex average: $average_round    &nbspstandard deviation: $stdev_round&nbspcoefficient of  variation(cv): $cv_round</b>"     ); }  sub average {     (@values) = @_;     $count    = scalar @values;     $total    = 0;     $total += $_ @values;      return $count ? $total / $count : 0; }  sub std_dev {     ( $average, @values ) = @_;      $count       = scalar @values;     $std_dev_sum = 0;     $std_dev_sum += ( $_ - $average )**2 @values;      return $count ? sqrt( $std_dev_sum / $count ) : 0; } 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -