my $i = 0; my %fullHash = map { ($i++, $_) } @rows; # Delete your stuff here my @keeplist = map { $fullhash{$_} } sort { $a <=> $b } keys %fullHash;