Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Transmit a while content to a foreach loop

Status
Not open for further replies.

tonguim

Programmer
May 12, 2009
1
0
0
FR
Hallo all,

i've an illegal division by zero error; i think that in the code, @elements1 and @elements1 are empty. Their content should normally come from the while loop "
while ((($debut, $fin) = $result =~ /debut\s+([0-9]+).+?fin\s+([0-9]+)/g) and $i < 2)
", but it does not. Anyone can help me fix the problem please?

#!/usr/bin/perl -w
use strict;
use warnings;
use diagnostics;
use CGI qw/:standard/;
use CGI::Carp 'fatalsToBrowser';

our $dir = '/chemin/Fichiers/';
our $meanOfLenghts1;
our $meanOfLenghts2;
our $mimetype;
our $numberOfFilesListed;
our $sum2;
our $sum1;
our $file;
our $i;
our @elements;
our @elements1;
our @elements2;
our $fh,
our @length;
#our $length;
our @listeFichiersMem;
our $debut;
our $fin;
our $line;
our $result;
our $value;

my $cgi = CGI -> new();

# read all entries in the directory:
opendir DIR, $dir or die "Cannot open $dir $!";
@listeFichiersMem = grep /\.txt$/, readdir DIR;
closedir DIR;
#printf $cgi -> header("Content-type: $mimetype; charset=utf-8; Content-Disposition: attachment;");

foreach $file (@listeFichiersMem)
{
printf "Hallo test";
$i = 0;
$numberOfFilesListed++;
open $fh, "<", $file or die $!;
print "$file" . "\n";

while ($line = $fh) #read each line from FILE.
{
chomp ($line);
while ((($debut, $fin) = $result =~ /debut\s+([0-9]+).+?fin\s+([0-9]+)/g) and $i < 2)
{
$length[$i] = $fin - $debut; # Calculation of the lenght of the first segment, then the lenght of the second segment
#push(@elements[$i], $length[$i]); #Push the 2 computed lenghts into a table to compute the mean of lenght for the 2 segments
$elements[$i] -> push($length[$i]);
$i++;
}
}
close $fh;
}

foreach $value (@elements1)
{
$sum1 += $value;
printf $sum1;
}

foreach $value (@elements2)
{
$sum2 += $value;
}

$meanOfLenghts1 = $sum1/$numberOfFilesListed;
$meanOfLenghts2 = $sum2/$numberOfFilesListed;

printf ("%d %d", $meanOfLenghts1, $meanOfLenghts2);
 
Without knowing what data you are running through your script its impossible to say.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
maybe do something like this to account for zero in the denominator
Code:
$meanOfLenghts1 = $numberOfFilesListed == 0 ? 0 : $sum1/$numberOfFilesListed;

I'm not sure how others handles this situation.


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
This line looks useless:

Code:
$elements[$i] -> push($length[$i]);

But I'm not sure what you are trying to do there so unsure what to suggest.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top