$hash = { k1 => [a1, b1, c1, d1], k2 => [a2, b2, c2, d1], k3 => [a3, b3, c3, d3] k4 => [a4, b4, c4, d3] } For the arrays where the d's are equal i want to do a numerical comparision on the a's and add a priority number at the end The output i expect $hash = { k1 => [a1, b1, c1, d1, 1], k2 => [a2, b2, c2, d1, 2], k3 => [a3, b3, c3, d3, 2] k4 => [a4, b4, c4, d3, 1] }