in reply to Read in values and average -- Perl for MacOS X

Hey,

first, I did not test the code, second I know this can be accomplished much shorter. Actually, I'm thinking about a oneliner.

Nevertheless, you are a newby and I suggest this code since it is much more readable.

#!/usr/bin/perl my %data; opendir( DIR, "input/*" ); while( my $file = readdir( DIR )) { open( FILE, $file ); while ($line = <FILE>) { chomp( $line ); $data { $file } -> { sum } += $line; $data { $file } -> { count } ++; } close( FILE ); } closedir( DIR ); open( FILE, ">", "result.txt" ); foreach my $file (sort keys %data) { printf FILE "%1.2f\n", $data { $file } -> { sum } / $data { $file +} -> { count }; } close( FILE );

Replies are listed 'Best First'.
Re^2: Read in values and average -- Perl for MacOS X
by briglass (Initiate) on Feb 14, 2005 at 21:49 UTC

    Thank you very much for your response!

    -Brian