snape has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I would like to know how can I read all the files in the directory. The files are of the same format. for eg:

A B 20 A C 15 C D 11 A D 10 C B 05

I need to know that how can I take the average of all the values of the column 3 across all the files in the directory and the directory contains more than 100 files.

Thanks.

Replies are listed 'Best First'.
Re: Reading all the files in the directory
by toolic (Bishop) on Feb 12, 2010 at 18:34 UTC
    Since you have not shown any code, I will assume you are just looking for an approach/algorithm:
    • Create a list of files using either glob or a combination of opendir, readdir, grep and -f.
    • Use a for loop and open each file.
    • Use a while loop to read in each line of the file.
    • split the line and keep a running sum of column 3 values. Also keep track of the number of lines.
    • After all lines of all files have been read, divide the sum by the total number of lines.
Re: Reading all the files in the directory
by crashtest (Curate) on Feb 12, 2010 at 18:58 UTC

    If that's all you need to do, your files are cleanly formatted, and you have a useful shell, you could do it in a one-liner:

    $ perl -nlae "$t+=$F[2]; END{print $t/$.}" *
    (see perlrun to understand the -a, -n, -l and -e options.)

    For a more flexible (and verbose) approach, the following could be a useful skeleton for your code:

    use strict; use warnings; use File::Find; sub process_file{ return unless (-f); # Only work on files, not directo +ries open INPUT, '<', $_ or die "Couldn't open $_: $!"; while (<INPUT>){ # works through the file line-by- +line chomp; # delete trailing new line my @columns = split /\s+/; # split on whitespace print "TODO: Process the following columns: ", join(" -- " => @columns), "\n"; } close INPUT or die $!; } # Find any file in a directory named "data", for example # (and its subdirectories) find(\&process_file, 'data');
    I am using File::Find in this example to process files. There are other ways to approach this.