Use your regular expression to capture the hops count for China. Every time China is found, add the value captured to an accumulator for total number of hops. Increment a separate variable by one each time China is found. After the loop terminates, divide the total number of hops by the number of times China was found. That's your average.
If you need to keep track of more than just China, store each country as a hash key, and let that hash point to anonymous arrays containing a "found" count and a "hops" total for that country. After the primary loop ends, loop over the countries stored in the hash, and for each one, do the division as described above.
If you're having trouble with capturing the hops, you're almost there, and will be 100% there once you skim through, perlrequick and perlretut, looking for how to use capturing parenthesis.
I doubt you're having trouble with the math. And it seems that you understand how to store a value in variables. The += operator will come in handy for your hops accumulator.
If you end up going the hash of arrays route, you might need to look at perlreftut, and maybe perldsc.
It would have saved a lot of typing for me to just provide the solution, but then (a) you never ascend the learning curve, (b) you never gain the pleasure and confidence that come with figuring it out yourself, and (c) your client would owe me, not you.
Dave
In reply to Re: Finding average from numbers appearing in a file
by davido
in thread Finding average from numbers appearing in a file
by maheshkumar
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |