The code would be easier to read ( and thus easier to comment on and make more efficent ) if you changed a couple things, first try using a little more descriptive names than i,j,k,m,n and o. Also as it doesn't appear that you modify the value of i/j or k/m within the context of one loop just get rid of j and m refer to them instead as (i+5) and (m+5).
I think also changing
for $R (0..$#ranges) to
foreach $range (i,i+5,m,m+5) it wont make your code run any faster, as others have noted above, reading in a 22MB file into an array, and itterating through that array that many times just isn't a good thing, but it's a good place to start.