diff --git a/README.md b/README.md index 0d9c12b..da81267 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Utility modules written in Perl, for Perl. - Author: Ethan Kletschke -- Version: `0.0.7` +- Version: `0.0.8` - Developed on: Ubuntu (WSL) - License: MIT - Project metadata file: [`project.yaml`](./meta/project.yaml) diff --git a/meta/BACKLOG.md b/meta/BACKLOG.md index e8428ff..ea14d2a 100644 --- a/meta/BACKLOG.md +++ b/meta/BACKLOG.md @@ -10,7 +10,8 @@ _Last Updated: 2026-11-01, 07:19am_ - [ ] `validate_csv()` - Validates that a file is a valid `.csv` file - [ ] `TextFiles::Simple` - - Must plan more simple text file methods + - [ ] `trunc_file()` subroutine + - [ ] `Arrays` Module - [ ] `Arrays::Simple` - [ ] `is_empty()` subroutine @@ -21,7 +22,7 @@ _Last Updated: 2026-11-01, 07:19am_ - Appends items to an array using a list of arbitrary arguments - [ ] `Arrays::Sets` - [ ] Move `union()` subroutine - - [ ] `intersection()` subroutine + - [x] `intersection()` subroutine - [ ] `is_subset()` subroutine - [ ] `difference()` subroutine - [ ] `symmetric_difference()` subroutine diff --git a/meta/project.yaml b/meta/project.yaml index a9b4ee9..f0a055c 100644 --- a/meta/project.yaml +++ b/meta/project.yaml @@ -1,12 +1,12 @@ project-meta: project-name: PerlUtils date-started: 2026-10-20 - last-updated: 2026-11-01 + last-updated: 2026-11-03 languages: - Perl status: Experimental license: MIT - version: 0.0.7 + version: 0.0.8 build: dev-environments: diff --git a/src/lib/Utils/Arrays/Sets.pm b/src/lib/Utils/Arrays/Sets.pm index fe256ef..e52a781 100644 --- a/src/lib/Utils/Arrays/Sets.pm +++ b/src/lib/Utils/Arrays/Sets.pm @@ -6,6 +6,7 @@ use Exporter 'import'; our @EXPORT_OK = qw( union + intersection ); sub union { @@ -45,6 +46,44 @@ sub union { return @union; } +sub intersection { + # Parameters + my ($arr1, $arr2) = @_; + + # Intersection array to return + my @intersect; + + # Tracking hash to keep track of what elements were encountered + # in both arrays. + my %trackingHash; + + # Loop through the first provided array + foreach my $element (@$arr1) { + # If the element was not previously encountered + if (not exists $trackingHash{$element}) { + # Set it to a true value in the tracking hash + $trackingHash{$element} = 1; + } + } + + # Loop through the second provided array + foreach my $element (@$arr2) { + # If the element was found in array 1 + if (exists $trackingHash{$element}) { + # Remove it from the tracking hash. + # This prevents duplicate elements in the + # second array from being added repeatedly + delete $trackingHash{$element}; + + # Add the element to the intersection + push @intersect, $element; + } + } + + # Return the intersection + return @intersect; +} + 1; __END__ @@ -77,6 +116,33 @@ It provides functions such as L, which finds the union of two arrays. =head2 union +Finds the union of both arrays provided, i.e., a collection of all +unique elements present in either array. + +=head3 PARAMETERS + +=over 4 + +=item C<$arr1> + +A reference to the first array. + +=item C<$arr2> + +A reference to the second array. + +=back + +=head3 RETURN VALUE + +Returns an array of the two provided arrays' +union elements. + +=head2 intersection + +Finds the intersection of two arrays, i.e., elements +present in both arrays, excluding duplicate values. + =head3 PARAMETERS =over 4 @@ -91,6 +157,20 @@ A reference to the second array. =back +=head3 RETURN VALUE + +Returns a new array containing elements present +in both arrays, without duplicates. + +=head3 EXAMPLE USAGE + + my @arr1 = (1, 2, 3, 4); + my @arr2 = (3, 4, 5, 6); + + my @intersect = intersection(\@arr1, \@arr2); + + print "@intersect\n"; # 3 4 + =head1 AUTHOR Ethan Kletschke (L) diff --git a/src/t/Arrays.t b/src/t/Arrays.t index 3d5fdca..90a1925 100644 --- a/src/t/Arrays.t +++ b/src/t/Arrays.t @@ -6,6 +6,7 @@ use Test::More; use Utils::Arrays::Sets qw( union + intersection ); subtest "Arrays::Sets" => sub { @@ -21,6 +22,8 @@ subtest "Arrays::Sets" => sub { [1, 2, 3, 4, 5, 6], "Correctly determines the union of two numeric arrays" ); + + done_testing(); }; subtest "String Arrays" => sub { @@ -33,8 +36,31 @@ subtest "Arrays::Sets" => sub { \@res, ["Rose", "Tom", "Dick", "Harry"], "Correctly determines the union of two string arrays" - ) + ); + + done_testing(); + }; + + done_testing(); + }; + + subtest "intersection()" => sub { + subtest "Numeric Arrays" => sub { + my @arr1 = (1, 2, 3, 4); + my @arr2 = (3, 4, 5, 6); + + my @intersect = intersection(\@arr1, \@arr2); + + is_deeply( + \@intersect, + [3, 4], + "Correctly determines the intersection of 2 arrays" + ); + + done_testing(); }; + + done_testing(); }; done_testing();