in reply to compound statement

the above logic is wrong because of its mistaken result, can you solve this algorithm ?

That's not very specific. What is the desired output? What is the actual output? And most importantly: how do your input files look like?

But I think the Problem is near the end of your code, where the foreach loop uses $_ implicitly as the loop variable, but you add $clearedValue to $sum, i.e. you are producing $n*$clearedValue, where $n is the number of items in the array @totalExoda