| 1 | package Array::Intersection; | ||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use List::MoreUtils qw{uniq}; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | require Exporter; | |||||||||
| 6 | 1 | our @ISA = qw(Exporter); | |||||||||
| 7 | 1 | our @EXPORT = qw(intersection); | |||||||||
| 8 | |||||||||||
| 9 | 1 | our $VERSION = '0.03'; | |||||||||
| 10 | 1 | our $UNDEF = "\000\001\002 QXJyYXk6OkludGVyc2VjdGlvbiBDb3B5cmlnaHQgKEMpIDIwMjQgYnkgTWljaGFlbCBSLiBEYXZpcw== \002\001\000"; #hash keys cannot be undef() | |||||||||
| 11 | |||||||||||
| 12 | =head1 NAME | ||||||||||
| 13 | |||||||||||
| 14 | Array::Intersection - Calculates the intersection of two array references | ||||||||||
| 15 | |||||||||||
| 16 | =head1 SYNOPSIS | ||||||||||
| 17 | |||||||||||
| 18 | use Array::Intersection; | ||||||||||
| 19 | my @intersection = intersection([1,2,3,4], [3,4,5,6]); #expect (3,4) | ||||||||||
| 20 | |||||||||||
| 21 | =head1 DESCRIPTION | ||||||||||
| 22 | |||||||||||
| 23 | This package exports the intersection() function which uses the magic of a hash slice to return an intersection of the data. | ||||||||||
| 24 | |||||||||||
| 25 | =head2 LIMITATIONS | ||||||||||
| 26 | |||||||||||
| 27 | Hash keys are strings so numeric data like 1 and 1.0 will be uniqued away in the string folding process. However, the function folds undef into a unique string so that it supports both empty string "" and undef. | ||||||||||
| 28 | |||||||||||
| 29 | =head2 FUNCTIONS | ||||||||||
| 30 | |||||||||||
| 31 | =head2 intersection | ||||||||||
| 32 | |||||||||||
| 33 | This intersection function uses a hash slice method to determine the intersection between the first array reference and the second array reference. | ||||||||||
| 34 | |||||||||||
| 35 | my @intersection = intersection([1,2,3,4], [3,4,5,6]); #expect (3,4) | ||||||||||
| 36 | my @intersection = intersection(\@array_1, \@array_2); | ||||||||||
| 37 | |||||||||||
| 38 | =cut | ||||||||||
| 39 | |||||||||||
| 40 | 11 | ▶ | sub intersection { | ||||||||
|
Subroutine
intersection: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||
| 41 | 11 | my $syntax = 'Syntax: my @output = intersection(\@array1, \@array2)'; | |||||||||
| 42 | 11 | ▶ | my $aref1 = shift or die($syntax); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | 10 | ▶ | my $aref2 = shift or die($syntax); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | 10 | ▶ | my %hash1 = map {defined ? ($_ => $_) : ($UNDEF => $UNDEF)} @$aref1; #fold undef into string, copy keys to values, and unique based on hash assignment | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | 9 | ▶ | my @uniq2 = uniq map {defined ? $_ : $UNDEF } @$aref2; #fold undef into string, and uniq to perserve order of second array | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | 8 | ▶ | my @intersection = map {$_ eq $UNDEF ? undef : $_} grep {defined} @hash1{@uniq2}; #hash slice for values, remove undef values where not exists, revert undef keys from sting | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | 8 | return @intersection; | |||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | =head1 SEE ALSO | ||||||||||
| 51 | |||||||||||
| 52 | https://perldoc.perl.org/perlfaq4#How-do-I-compute-the-difference-of-two-arrays?-How-do-I-compute-the-intersection-of-two-arrays? | ||||||||||
| 53 | L<List::MoreUtils> | ||||||||||
| 54 | |||||||||||
| 55 | =head1 AUTHOR | ||||||||||
| 56 | |||||||||||
| 57 | Michael R. Davis | ||||||||||
| 58 | |||||||||||
| 59 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 60 | |||||||||||
| 61 | Copyright (C) 2024 by Michael R. Davis | ||||||||||
| 62 | |||||||||||
| 63 | MIT | ||||||||||
| 64 | |||||||||||
| 65 | =cut | ||||||||||
| 66 | |||||||||||
| 67 | 1 | 1; | |||||||||