Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    New to the CF scene
    Join Date
    May 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Transmit a while content to a foreach loop

    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);
    Last edited by tonguim; 05-12-2009 at 08:48 PM.


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •