From dd3f018176a1241cc14714acb2095487b06427f6 Mon Sep 17 00:00:00 2001 From: Guillem Jover Date: Tue, 29 Oct 2019 18:22:51 +0000 Subject: [PATCH] TT#69200 Reimplement removeduplicates() based of List::Util's uniq This adds a behavior change, reflected in the unit test changes, by always returning the lowercased versions of the array elements when case insensitiveness is enabled. This should be fine, given that we do not care from the callers PoV, and the result might have changed depending on the order of the inputs. Change-Id: I44896183b566e921cc1287f106fb4bb9332acb97 (cherry picked from commit 94853682904108147363cfa2b7bcdb25f7003ae2) --- lib/NGCP/BulkProcessor/Array.pm | 23 ++++++++++++----------- t/Array.t | 8 ++++---- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/lib/NGCP/BulkProcessor/Array.pm b/lib/NGCP/BulkProcessor/Array.pm index 90c1265..883eb06 100644 --- a/lib/NGCP/BulkProcessor/Array.pm +++ b/lib/NGCP/BulkProcessor/Array.pm @@ -3,7 +3,7 @@ use strict; ## no critic -use List::Util qw(any); +use List::Util qw(any uniq); use NGCP::BulkProcessor::Table; @@ -40,18 +40,19 @@ sub mergearrays { } sub removeduplicates { + my ($array_ptr, $case_insensitive) = @_; - my ($array_ptr,$case_insensitive) = @_; - my @result = (); - if (defined $array_ptr and ref $array_ptr eq 'ARRAY') { - - foreach my $element (@$array_ptr) { - if (not contains($element,\@result,$case_insensitive)) { - push @result,$element; - } + my @result; + if (defined $array_ptr and ref $array_ptr eq 'ARRAY') { + if ($case_insensitive) { + @result = map { lc } @{$array_ptr}; + } else { + @result = @{$array_ptr}; + } + @result = uniq @result; } - } - return \@result; + + return \@result; } diff --git a/t/Array.t b/t/Array.t index 1fe735e..7367cf9 100644 --- a/t/Array.t +++ b/t/Array.t @@ -53,7 +53,7 @@ is_deeply(removeduplicates([ qw(aa bb aa cc aa) ], 0), [ qw(aa bb cc) ]); is_deeply(removeduplicates([ qw(aa aa bb bb cc cc) ], 0), [ qw(aa bb cc) ]); is_deeply(removeduplicates([ qw(aa bb aa cc bb cc) ], 0), [ qw(aa bb cc) ]); -is_deeply(removeduplicates([ qw(Aa BB cC) ], 1), [ qw(Aa BB cC) ]); -is_deeply(removeduplicates([ qw(aA BB Aa Cc aa) ], 1), [ qw(aA BB Cc) ]); -is_deeply(removeduplicates([ qw(aA AA bB Bb CC cc) ], 1), [ qw(aA bB CC) ]); -is_deeply(removeduplicates([ qw(AA bB Aa cc Bb CC) ], 1), [ qw(AA bB cc) ]); +is_deeply(removeduplicates([ qw(Aa BB cC) ], 1), [ qw(aa bb cc) ]); +is_deeply(removeduplicates([ qw(aA BB Aa Cc aa) ], 1), [ qw(aa bb cc) ]); +is_deeply(removeduplicates([ qw(aA AA bB Bb CC cc) ], 1), [ qw(aa bb cc) ]); +is_deeply(removeduplicates([ qw(AA bB Aa cc Bb CC) ], 1), [ qw(aa bb cc) ]);