commit 1b881234def2a09fbf64865f959ab45e658aaa30
parent 9f372bd9e5a26b55c1272d181d18b5abe090ee84
Author: Samir Parikh <noreply@samirparikh.com>
Date: Wed, 22 Dec 2021 15:30:21 +0000
update Sublist.pm
Diffstat:
1 file changed, 14 insertions(+), 4 deletions(-)
diff --git a/sublist/Sublist.pm b/sublist/Sublist.pm
@@ -5,20 +5,30 @@ use v5.22;
use Exporter qw<import>;
our @EXPORT_OK = qw<compare_lists>;
-sub a_within_b {
+sub a_within_b2 {
my ($a, $b) = @_;
my @a = @{$a};
my @b = @{$b};
foreach my $element (@a) {
- #print "checking $_ within @b ... ";
- #my $found = grep { /$_/ } @b;
- #say "found $found";
return 0 unless (grep /^$element$/, @b); # grep needs expr that evaluates
# to true or false
}
return 1;
}
+sub a_within_b { # assume @a <= @b
+ my ($a, $b) = @_;
+ my @a = @{$a};
+ my @b = @{$b};
+ # check to see if first element of @a is in @b
+ my @idx = grep { $b[$_] eq $a[0] } 0 .. $#b;
+ return 0 unless (@idx);
+ for (my $a_idx=1, my $b_idx=$idx[0]+1; $a_idx<=$#a; $a_idx++, $b_idx++) {
+ return 0 unless ($a[$a_idx] eq $b[$b_idx]);
+ }
+ return 1;
+}
+
sub compare_lists {
my ($args) = @_;
my @a = @{$args->{listOne}};