my %presence; my $b = 1; foreach my $ary (\@list1, \@list2, \@list3) { foreach(@$ary) { $presence{$_} |= $b; } } continue { $b *= 2; }