my %keep; @keep{@files} = 1; # populate a lookup hash foreach my $file ( @filelist ) { unlink $file unless exists( $keep{$file} ); }