use strict; my @a = (1..20); my @b = (2,3,13,14,17); @a = do{my %i; undef @i{@a}; delete @i{@b}; sort {$a<=>$b} keys %i};