use strict; use warnings; my @nums = (1000..9999); my @picks = grep {my %d; map {$d{$_}++} split ''; grep {$_ == 2} values %d} @nums; print join "\n", @picks;