From 11ab782813ab0a4d5420d463eef60093bc29bdf8 Mon Sep 17 00:00:00 2001 From: Ethan Date: Mon, 2 Nov 2026 11:54:16 +0200 Subject: [PATCH 1/6] fix(pod): Finish `union()` POD --- src/lib/Utils/Arrays/Sets.pm | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/lib/Utils/Arrays/Sets.pm b/src/lib/Utils/Arrays/Sets.pm index fe256ef..e8fc703 100644 --- a/src/lib/Utils/Arrays/Sets.pm +++ b/src/lib/Utils/Arrays/Sets.pm @@ -77,6 +77,9 @@ 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 @@ -91,6 +94,11 @@ A reference to the second array. =back +=head3 RETURN VALUE + +Returns an array of the two provided arrays' +union elements. + =head1 AUTHOR Ethan Kletschke (L) From 2f67062664646d0947b9ff763df267fb9c4550b0 Mon Sep 17 00:00:00 2001 From: Ethan Date: Mon, 2 Nov 2026 12:01:58 +0200 Subject: [PATCH 2/6] feat: Create `intersection()` subroutine --- src/lib/Utils/Arrays/Sets.pm | 38 ++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/lib/Utils/Arrays/Sets.pm b/src/lib/Utils/Arrays/Sets.pm index e8fc703..2643854 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,43 @@ 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 + push @intersect, $element; + } + } + + # Return the intersection + return @intersect; +} + 1; __END__ From 849fe3dc7afa99a9a9b27c6c51b42343f5107a0f Mon Sep 17 00:00:00 2001 From: Ethan Date: Mon, 2 Nov 2026 12:06:34 +0200 Subject: [PATCH 3/6] docs(pod): Add POD for `intersection()` --- src/lib/Utils/Arrays/Sets.pm | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/lib/Utils/Arrays/Sets.pm b/src/lib/Utils/Arrays/Sets.pm index 2643854..9dee6a1 100644 --- a/src/lib/Utils/Arrays/Sets.pm +++ b/src/lib/Utils/Arrays/Sets.pm @@ -137,6 +137,39 @@ A reference to the second array. 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 + +=item C<$arr1> + +A reference to the first array. + +=item C<$arr2> + +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) From 43d7523a8ae9418ba143bf538d8695e743787747 Mon Sep 17 00:00:00 2001 From: Ethan Date: Tue, 3 Nov 2026 07:48:52 +0200 Subject: [PATCH 4/6] fix(docs): Fix unfinished comment --- src/lib/Utils/Arrays/Sets.pm | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib/Utils/Arrays/Sets.pm b/src/lib/Utils/Arrays/Sets.pm index 9dee6a1..e52a781 100644 --- a/src/lib/Utils/Arrays/Sets.pm +++ b/src/lib/Utils/Arrays/Sets.pm @@ -74,7 +74,8 @@ sub intersection { # This prevents duplicate elements in the # second array from being added repeatedly delete $trackingHash{$element}; - # Add + + # Add the element to the intersection push @intersect, $element; } } From 5536b79dd915907977c38c22ade8ca7633cc8af5 Mon Sep 17 00:00:00 2001 From: Ethan Date: Tue, 3 Nov 2026 07:50:05 +0200 Subject: [PATCH 5/6] docs: Update version numbers and `BACKLOG` --- README.md | 2 +- meta/BACKLOG.md | 5 +++-- meta/project.yaml | 4 ++-- 3 files changed, 6 insertions(+), 5 deletions(-) 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: From 59245f99815e51d41a726ebc3b736e207db1d478 Mon Sep 17 00:00:00 2001 From: Ethan Date: Tue, 3 Nov 2026 07:57:07 +0200 Subject: [PATCH 6/6] tests: Add module test for `intersection()` sub --- src/t/Arrays.t | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) 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();