diff --git a/README.md b/README.md index 476073e..62e2b39 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Utility modules written in Perl, for Perl. - Author: Ethan Kletschke -- Version: `0.0.5` +- Version: `0.0.6` - Developed on: Ubuntu (WSL) - License: MIT - Project metadata file: [`project.yaml`](./meta/project.yaml) diff --git a/meta/project.yaml b/meta/project.yaml index bfcf027..ff0694d 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-10-31 + last-updated: 2026-11-01 languages: - Perl status: Experimental license: MIT - version: 0.0.5 + version: 0.0.6 build: dev-environments: diff --git a/src/lib/Utils/Arrays/Simple.pm b/src/lib/Utils/Arrays/Simple.pm new file mode 100644 index 0000000..1fa625a --- /dev/null +++ b/src/lib/Utils/Arrays/Simple.pm @@ -0,0 +1,48 @@ +package Utils::Arrays::Simple; + +use strict; +use warnings; +use Exporter 'import'; + +our @EXPORT_OK = qw( + union +); + +sub union { + # Parameters + my ($arr1, $arr2) = @_; + + # Result array + my @union; + # Hash for tracking elements + my %trackingHash; + + # Loop through the first array + foreach my $element (@$arr1) { + # If the element was not previously encountered + if (not exists $trackingHash{$element}) { + # Add a truthy value for that element to the tracking hash + $trackingHash{$element} = 1; + # Push the element to the result array + push @union, $element; + } + } + + # Loop through the second array + foreach my $element (@$arr2) { + # If the element was not previously encountered + if (not exists $trackingHash{$element}) { + # Add a truthy value for that element to the tracking hash + $trackingHash{$element} = 1; + # Push the element to the result array + push @union, $element; + } + } + + # Return the result array + return @union; +} + +1; + +__END__ diff --git a/src/t/Arrays.t b/src/t/Arrays.t new file mode 100644 index 0000000..0bc46d7 --- /dev/null +++ b/src/t/Arrays.t @@ -0,0 +1,25 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use Test::More; + +use Utils::Arrays::Simple qw( + union +); + +subtest "Arrays::Simple" => sub { + subtest "union()" => sub { + my @arr1 = (1, 2, 3, 4); + my @arr2 = (3, 4, 5, 6); + + my @res = union(\@arr1, \@arr2); + + is_deeply(\@res, [1, 2, 3, 4, 5, 6], "Correctly determines the union"); + }; + + done_testing(); +}; + + +done_testing(); diff --git a/src/t/TextFiles.t b/src/t/TextFiles.t index b09f5c4..ddfe62e 100644 --- a/src/t/TextFiles.t +++ b/src/t/TextFiles.t @@ -1,6 +1,5 @@ #!/usr/bin/env perl -use lib 'lib'; use strict; use warnings; use Test::More;