for (my $x = $#temp; $x >= 0; $x--) { if ( substr($temp[$x-1][0], 0, -1) eq substr($temp[$x][0], 0, -1)) { $temp[$x-1][1] += $temp[$x][1]; splice @temp, $x, 1; } }