my %x; $x{$_} = undef for @selected_data; my %y; $y{$_} = undef for @all_data; my $total; foreach my $k ( keys %x ) { $total++ if exists $y{$k}; } print "Count -> $total\n";