#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11152381 use warnings; my %sets = ( A => [1,2,3], B => [3, 4], C => [1, 3, 4] ); use Data::Dump 'dd'; dd 'original sets', \%sets; my %intersection; for my $setname ( keys %sets ) { $intersection{$_}{$setname}++ for @{ $sets{$setname} }; } use Data::Dump 'dd'; dd 'intersection', \%intersection; print "items in more than one set:\n"; for my $setname ( sort keys %intersection ) { if( 1 < keys %{ $intersection{$setname} } ) { my $where = join ' and ', map "'$_'", sort keys %{ $intersection{$setname} }; print " '$setname' is in $where\n"; } } #### ( "original sets", { A => [1, 2, 3], B => [3, 4], C => [1, 3, 4] }, ) ( "intersection", { 1 => { A => 1, C => 1 }, 2 => { A => 1 }, 3 => { A => 1, B => 1, C => 1 }, 4 => { B => 1, C => 1 }, }, ) items in more than one set: '1' is in 'A' and 'C' '3' is in 'A' and 'B' and 'C' '4' is in 'B' and 'C'