Let me add a few comments and suggestions:
I'm not saying that this is a better solution, I'd go - for the sake of readability/maintainability - for something straight forward like your first version.#!/usr/bin/perl use strict; use warnings; # always check if open fails open(DB, '<test.data') or die "Couldn't open file: $!"; my @numbers = map { my ($prod, $num, $dom) = (split /:/)[36,38,40]; (unpack("A4",$dom) eq 'ATM' && $prod eq 'In Production')?$num:() } <DB>; close DB; my $sum; $sum += $_ foreach (@numbers); printf "The average is %.2f.", @numbers ? ($sum/@numbers) : 0;
-- Hofmator
In reply to Re: Re: Calculating the average of a column in a flat-file database
by Hofmator
in thread Calculating the average of a column in a flat-file database
by suggus
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |