in reply to sorting an array

Change
if ($linkscount == 4) { $linkscount = 0; }
to
if ($linkscount == 3) { $linkscount = 0; }
because you increment the counter after you do this test.

And your code works in a way that feels very "unperlish" to me, but I'm too lazy to completely rewrite it right now. Perhaps later. For example, there's no need to use 3 independent arrays, use an array of arrays instead, and directly use the counter as an index for what subarray to use.

push @{$links[$linkscount]}, $_;
And I'd prefer to let $linkscount go from 0 to 2, instead of from 1 to 3. Because now you'll get an empty slot for index 0. All you have to do is swap the $linkscount++ statement and the above test.