#!/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"; } }