Peter’s blog ✴ Week 373 ✴ 11 May 2026

THE WEEKLY CHALLENGE
The week of lists

The Perl Camel

Task 1

Equal list

You are given two arrays of strings. Write a script to return true if the two given arrays represent the same strings when concatenated, otherwise return false.

Examples


Example 1
Input: @arr1 = ('a', 'bc')
       @arr2 = ('ab', 'c')
Output: true
Array 1: 'a' + 'bc' = 'abc'
Array 2: 'ab' + 'c' = 'abc'

Example 2
Input: @arr1 = ('a', 'b', 'c')
       @arr2 = ('a', 'bc')
Output: true
Array 1: 'a' + 'b' + 'c' = 'abc'
Array 2: 'a' + 'bc' = 'abc'

Example 3
Input: @arr1 = ('a', 'bc')
       @arr2 = ('a', 'c', 'b')
Output: false
Array 1: 'a' + 'bc' = 'abc'
Array 2: 'a' + 'c' + 'b' = 'acb'

Example 4
Input: @arr1 = ('ab', 'c', '')
       @arr2 = ('', 'a', 'bc')
Output: true
Array 1: 'ab' + 'c' + '' = 'abc'
Array 2: ''  + 'a' + 'bc' = 'abc'

Example 5
Input: @arr1 = ('p', 'e', 'r', 'l')
       @arr2 = ('perl')
Output: true
Array 1: 'p' + 'e' + 'r' + 'l' = 'perl'
Array 2: 'perl'

Analysis

As always, someone will have a one-liner, but I settled initially for two lines. However, in order to get rid of the 'unitiallised' warning where either concatenated string was empty I had to add a third line.

Perl Weekly’s review

from PW issue 773

This paragraph describes the outstanding quality of Peter's work in executing his weekly assignments and projects using clear and detailed methods. He concentrates on providing clear and easy to understand explanations of all aspects of the problem using very pragmatic solutions with less complex algorithms than would be required to complete this task, and thus have produced solutions with low run times and high readability. Another benefit of using this strategy is that it will have produced many examples of good clean and maintainable code, as well as simple Perl code showing how to accomplish structural validation without additional runtime cost.

Try it 

Your input:



eg: 'this', 'is', 'true'



eg: 'thisis', 'true'

Script


#!/usr/bin/perl

# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge

use v5.26;    # The Weekly Challenge - 2026-05-11
use utf8;     # Week 373 - task 1 - Equal list
use warnings; # Peter Campbell Smith
binmode STDOUT, ':utf8';
use Encode;

equal_list(['a', 'bc'], ['a', 'bc']);
equal_list(['a', 'c', 'b'], ['abc']);
equal_list(['ab', 'c'], []);
equal_list(['', ''], ['', '', '']);
equal_list(['c', 'h', 'al', 'le', 'ng', 'e'], ['challenge']);
equal_list(['cha', 'llenge'], ['chall', 'enge']);

sub equal_list {
    
    my ($s, @joined);
    
    # concatenate the string elements
    @joined = ('', '');
    $joined[0] .= $_ for @{$_[0]};
    $joined[1] .= $_ for @{$_[1]};
    
    # report
    say qq[\nInput:  ('] . join(q[', '], @{$_[0]}) . q['), ('] . 
        join(q[', '], @{$_[1]}) . q[')];
    say qq[Output: ] . ($joined[0] eq $joined[1] ? 'true' : 'false');
}

8 lines of code

Output from script


Input:  ('a', 'bc'), ('a', 'bc')
Output: true

Input:  ('a', 'c', 'b'), ('abc')
Output: false

Input:  ('ab', 'c'), ('')
Output: false

Input:  ('', ''), ('', '', '')
Output: true

Input:  ('c', 'h', 'al', 'le', 'ng', 'e'), ('challenge')
Output: true

Input:  ('cha', 'llenge'), ('chall', 'enge')
Output: true

 

Any content of this website which has been created by Peter Campbell Smith is in the public domain