Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions meta/BACKLOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
4 changes: 2 additions & 2 deletions meta/project.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
80 changes: 80 additions & 0 deletions src/lib/Utils/Arrays/Sets.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use Exporter 'import';

our @EXPORT_OK = qw(
union
intersection
);

sub union {
Expand Down Expand Up @@ -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__
Expand Down Expand Up @@ -77,6 +116,33 @@ It provides functions such as L</union>, 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
Expand All @@ -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<mailto:ethankletschke@outlook.com>)
Expand Down
28 changes: 27 additions & 1 deletion src/t/Arrays.t
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use Test::More;

use Utils::Arrays::Sets qw(
union
intersection
);

subtest "Arrays::Sets" => sub {
Expand All @@ -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 {
Expand All @@ -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();
Expand Down
Loading