my $loop_index = 0; for $comma_array (@current_case_commas) { for my $comma (@$comma_array) { if($comma != "") { for (my $i=0; $i < $loop_index; $i++) { my @other_cases_array = @{$current_case_commas[$i]}; for my $other_cases (@other_cases_array) { if($other_cases = "") { if ($comma == $other_cases) { $final_case_values_duplicate{$comma} = (); } } } } for (my $i=$loop_index+1; $i <= $#current_case_commas; $i++) { my @other_cases_array = @{$current_case_commas[$i]}; for my $other_cases (@other_cases_array) { if($other_cases != "") { if ($comma == $other_cases) { $final_case_values_duplicate{$comma} = (); } } } } } } $loop_index+=1; }