in reply to going loopy

Update: Broquaint's response tells me that I should have been looking farther up in the code for a similar problem to the one that I noticed. I thought that you might be doing the iteration over the @unique_slopes array outside of all your loops, and therefore, the problem listed below exists. In my haste, I neglected to see that a similar problem existed with the other two arrays. My appologies.

It appears to me that you are resetting @unique_slopes each time in the iteration. If my assumptions are correct, change it to (untested):

push @unique_slopes, (grep !$seen2{$_}++, @slopes);

And that should solve the problem.