my %info; $info{$_} .= "A" for keys %A; $info{$_} .= "B" for keys %B; for (sort keys %info) { print "$_ is in: $info{$_}\n"; }