| 1 | #=============================================== | ||||||||||||||||||||||||||||
| 2 | package Banal::Utils::Array; | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | no warnings qw(uninitialized); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | require Exporter; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 2 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 13 | 2 | our @EXPORT_OK = qw(array1_starts_with_array2); | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | # Returns true (1) if array1 starts with array2 (via element-by-element equality), false (0) otherwise. | ||||||||||||||||||||||||||||
| 16 | # The order of elements is important (it ain't a bag). For each compared element, equality can be established either thru string (eq) or numeric (==) comparison. | ||||||||||||||||||||||||||||
| 17 | 0 | ▶ | sub array1_starts_with_array2 { | ||||||||||||||||||||||||||
|
Subroutine
array1_starts_with_array2: not called CC=6 SCAR=33.6
Pod
documented
| |||||||||||||||||||||||||||||
| 18 | 0 | my ($array1, $array2) = @_; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | # if array2 is empty, then the result is undefined. | ||||||||||||||||||||||||||||
| 21 | 0 | ▶ | return unless (scalar($array2)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | # if array2 has more items than array1, then array1 can't possibly start with array2. | ||||||||||||||||||||||||||||
| 24 | 0 | ▶ | return 0 if (scalar($array2) > scalar($array1)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 0 | my $k = 0; | |||||||||||||||||||||||||||
| 27 | 0 | foreach my $e2 (@$array2) { | |||||||||||||||||||||||||||
| 28 | 2 | ▶ | no warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 0 | my $e1 = $array1->[$k]; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | # Here, we allow both string and numeric equality. It's kind o a relaxed thing. The "no warnings" pragma (above) comes in handy. | ||||||||||||||||||||||||||||
| 32 | 0 | ▶ | return 0 unless (($e1 eq $e2) || ($e1 == $e2)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$e1 eq $e2 or $e1 == $e2
MC/DC0%0/2
$e1 eq $e2 or $e1 == $e2
$e1 eq $e2
$e1 == $e2
Truth table0%0/3
A or B
A= $e1 eq $e2B= $e1 == $e2
| |||||||||||||||||||||||||||||
| 33 | 0 | $k++; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | 0 | return 1; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 2 | 1; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | __END__ | ||||||||||||||||||||||||||||