Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: Finding average from numbers appearing in a file

by trizen (Hermit)
on Aug 05, 2012 at 08:44 UTC ( #985507=note: print w/replies, xml ) Need Help??

in reply to Finding average from numbers appearing in a file

use strict; use warnings; use Locale::Country; my @names = all_country_names(); # Better use Regexp::Trie my $countries_re = do { local $" = q{|}; qr/@{[map quotemeta, @names]}/i; }; my %hash; while (<>) { #if (/^.*?-([A-Z][A-Za-z\s\-]+[a-z])\s+(\d+)\s+hops?\b/is) { if (/^.*?-($countries_re)\s+(\d+)\s+hops?\b/is) { my ($country, $hops) = ($1, $2); $hash{$country} = { sum => ($hash{$country}{sum} || 0) + $hops, num => ++$hash{$country}{num} }; } } foreach my $key (sort keys %hash) { printf "$key avg: %.3f\n", $hash{$key}{sum} / $hash{$key}{num}; } __END__ Australia avg: 30.000 China avg: 22.667 Czech Republic avg: 19.500 France avg: 30.000 Germany avg: 8.500 Japan avg: 18.000 Netherlands avg: 9.500 United Kingdom avg: 8.000 United States avg: 19.667

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://985507]
[Your Mother]: The exhibit, the tick, or the scrotum?
[erix]: (the man is even a paleontologist )
[1nickt]: erix unfortunately that's there favourite spot. That's why they are usually spotted crawling *up* your leg.
[erix]: ha, the tick and, as it happens, also the curator (now a professor of Paleont.)
[1nickt]: s/there/their/, ugh sorry.
[Your Mother]: Heh. The only one I ever had on me, I caught on my shin, headed north.
erix looks nervously I got away cleanly from my run , it seems :)
1nickt doesn;t remember there being ticks in the old country ...
erix too fast for ticks!
[erix]: 'the old country' is where?

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (15)
As of 2017-05-24 13:16 GMT
Find Nodes?
    Voting Booth?