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;