Sorting and manipulating arrays perl