...

View Full Version : Transmit a while content to a foreach loop



tonguim
05-12-2009, 07:45 PM
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);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum