Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Set intersection problem

by tybalt89 (Monsignor)
on May 23, 2023 at 15:00 UTC ( [id://11152393]=note: print w/replies, xml ) Need Help??


in reply to Set intersection problem

I'm not sure I understand your problem, but my guess goes something like this:

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

Outputs (complete with intermediate calculations):

( "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'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11152393]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-09-17 06:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The PerlMonks site front end has:





    Results (22 votes). Check out past polls.

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.