sub remove_dup { my @arr = shift; my %seen; for ( my $i = 0; $i <= $#{$arr}; ){ splice @$arr, --$i, 1 if $seen{$arr->[ $i++]}++; } }