in reply to Finding average from numbers appearing in a file
Using the text file given in the above question or request, if I may give you a head up.
So is it possible that i can find the average
Yes, it is Possible!
Say, you want to find the average for country china only , you might do something like so:
However, it becomes a whole new ball game, if you are considering, doing the same for all the countries in the file. But that also is not difficult, as it may look.use warnings; use strict; my $total_number_of_hops = 0; my $number_of_matches = 0; while (<>) { chomp; if (/.+?-(china\s+?[0-9]+?\s+?hops?)/is) { ++$number_of_matches; my ( $country, $number_of_hops, $hop ) = split /\s+/, $1, 3; $total_number_of_hops += $number_of_hops; } } printf "Average Number of Hops for China is %.2f", $total_number_of_hops / $number_of_matches;
use warnings; use strict; use Data::Dumper; my $country_hop_ref = {}; while (<>) { chomp; if (/.+?-(([a-z\s+]+?)\s+?([0-9]+?)\s+?hops?)/is) { push @{ $country_hop_ref->{$2} }, $3; } } print Dumper $country_hop_ref;
That is the head up, you need to find the average, yourself!!!
Please check the following:
|
|---|