SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Oct 2005
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy Read a folder and files

    Hi, i would like to write a program that read in a directory all the files ended by ".txt". I have to check into each file, in each line, the first integer values that follow the word "begin" and the word "end" and make the difference between them, to get a distance value, and the same with the second line. I have some difficulties with the code; can you help me please?

    Code Perl:
    #!/usr/bin/perl -w
    #use strict;
    use warnings;
     
    use CGI::Carp qw(fatalsToBrowser);
     
    my $dir = './Chemin/Fichier/';
     
    # read all entries in the directory:
    opendir DIR, $dir or die "Cannot open $dir $!";
    my @listeFichiersMem = grep /\.txt$/, readdir DIR;
    foreach $file (@listeFichiersMem) 
    {
    	open FILE, $file or die $!;
      print $file . "\n";
    	while (my $line = <FILE>) #read each line and check the first value X1 after the word "coorDeb" and the first value X2 after the word "coorFin"
    	{
    		$line1  =~ m{begin}/(\s*)\S*\s*\S*\s*\S*\s*\S*\s*(\S*/); #begin first, following by X1
     
    		$X1=$S1; $X4=$S4;
    		$lenght1= $X1-$X2; # Calculation of the lenght of first segment
     
    		$line1  =~ m{end}/(\s*)\S*\s*\S*\s*\S*\s*\S*\s*(\S*/); #end first, following by X1
    		$X1=$S11; $X4=$S14;
    		$lenght2= $X1-$X2; # Calculation of the lenght of first segment
     
    	}
    		push(@elements1, $lenght1); #Push the lenght into a table to compute the mean of lenght for the segment n°1
    		push(@elements2, $lenght2); #Push the lenght into a table to compute the mean of lenght for the segment n°2
    }
     
    foreach (@elements1)
    {
    	    $sum1 += $_;
    }
     
    foreach (@elements2)
    {
    	    $sum2 += $_;
    }
     
    $meanOfLenghts1=$sum1/2;
    $meanOfLenghts2=$sum2/2;
    closedir DIR;

    Each file has the same following content,

    Code Perl:
    (champs1 (champs6 donnee_o donnee_f) [(champs2 [] (champs3 _YOJNJeyyyyyyB (champs4 donnee_x)) (begin 144825 25345) (end 244102 40647)), (champs2 [] (champs3 _FuGNJeyyyyyyB (champs4 donnee_z)) (begin 796443 190570) (end 145247 42663))] [] []).
    Last edited by tonguim; May 11, 2009 at 23:41.

  2. #2
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    why does the script search for "coordeb" and coorfin" if you say you want to search for "begin" and "end" and then the line you posted from the file appears to not have those words in it?

  3. #3
    SitePoint Member
    Join Date
    Oct 2005
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm sorry Kevin,

    you're right. I've updated the script. But the matter doesn't come from this error.

  4. #4
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    there is no "begin" and "end" in the data sample you posted.

  5. #5
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ALso, your script is reading the lines into $line:

    while (my $line = <FILE>)

    yet your code never searches in $line for the patterns

  6. #6
    SitePoint Member
    Join Date
    Oct 2005
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi 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 < 3)
    , but does not.

    Code:
    #!/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 = 1;
    		$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 < 3)
              {
                            $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 ("&#37;d %d", $meanOfLenghts1, $meanOfLenghts2);
    Last edited by tonguim; May 12, 2009 at 12:46.


Bookmarks

Posting Permissions

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