Here's a rough draft (working properly, however) of one way to do it. I'm using a couple of hashes to reduce each array to simply a count of each element. Then the lesser of the two counts becomes the number of times set three should contain a given element.
use strict; use warnings; use List::Util qw/min/; my @list_a = qw/test test1 test2 test2 test/; my @list_b = qw/test1 test2 test2/; my( %element_count_a, %element_count_b ); %element_count_a = %{ count( \@list_a ) }; %element_count_b = %{ count( \@list_b ) }; foreach my $key ( keys %element_count_a ) { foreach ( 1 .. min( $element_count_a{$key}, exists( $element_count_b{$key} ) ? $element_count_b{$key} : '0' ) ) { print $key, "\n"; } } sub count { my( $list_aref ) = shift; my %element_count; foreach my $item ( @{ $list_aref } ) { $element_count{$item}++; } return \%element_count; }
Updated to choose better variable names.
Dave
In reply to Re: Compare Lists (Non Unique Intersection)
by davido
in thread Compare Lists (Non Unique Intersection)
by rsiedl
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |