Peter’s blog ✴ Week 324 ✴ 2 June 2025

THE WEEKLY CHALLENGE
Fun with arrays

The Perl Camel

Task 1

2d array

You are given an array of integers and two integers $r and $c. Write a script to create a two dimensional array having $r rows and $c columns, using the given array.

Examples


Example 1
Input: @ints = (1, 2, 3, 4), $r = 2, $c = 2
Output: ([1, 2], [3, 4])

Example 2
Input: @ints = (1, 2, 3), $r = 1, $c = 3
Output: ([1, 2, 3])

Example 3
Input: @ints = (1, 2, 3, 4), $r = 4, $c = 1
Output: ([1], [2], [3], [4])

Analysis

This is an easy challenge as stated, but I like to show a two-dimensional array in two dimensions, which is slightly tricky when the elements can be of different lengths. Fortunately I've done that before, most recently in week 299, so I just reused that code.

Also any solution will trip up if the input array contains anything other than $r * $c elements, so I've checked that first.

Perl Weekly’s review

from Perl Weekly issue 724

The post stands out for its clarity, depth, and instructional value. His explanations are accessible, making complex concepts understandable for a broad audience. The code is well-structured and annotated, providing readers with valuable insights into Perl programming and problem-solving techniques.

Try it 

Try running the script with any input:



example: 1, 2, 3, 4, 5, 6



example: 3, 2

Script


#!/usr/bin/perl

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

use v5.26;    # The Weekly Challenge - 2025-06-02
use utf8;     # Week 324 - task 1 - 2d array
use warnings; # Peter Campbell Smith
binmode STDOUT, ':utf8';
use Encode;

two_d_array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], 3, 4);
two_d_array([1, 2, 3], 1, 3);
two_d_array([1, 2, 3, 4], 4, 1);

# bigger example
my $matrix;
push @$matrix, int(rand(1000)) for 0 .. 76;
two_d_array($matrix, 11, 7);

sub two_d_array {
    
    my ($integers, $rr, $cc, $j, $r, $c, $array, $w);
    
    # initialise
    ($integers, $rr, $cc) = @_;
    if (@$integers != $rr * $cc) {
        say qq[\@integers must have $rr * $cc elements];
        exit;
    }
    
    # get width of largest number for formatting matrix
    $w = 0;
    $w = length($_) > $w ? length($_) : $w for @$integers;
    
    # fill in matrix
    for $j (0 .. @$integers - 1) {
        $r = int($j / $cc);
        $c = $j % $cc;
        $array->[$r]->[$c] = sprintf("%${w}d", $integers->[$j]);
    }
    
    # report
    say qq[\nInput:  \@integers = (] . join(', ', @$integers) . qq[), \$rows = $rr, \$cols = $cc];
    print_matrix('Output: @matrix =  ', $array);
}

sub print_matrix {
    
    my ($legend, $matrix, $j);

    # format matrix
    ($legend, $matrix) = @_;
    for $j (0 .. @$matrix - 1) {
        say qq{$legend [} . join(', ', @{$matrix->[$j]}) . qq(]);
        $legend = ' ' x length($legend);
    }
}


20 lines of code

Output from script


Input:  @integers = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
   12), $rows = 3, $cols = 4
Output: @matrix =   [ 1,  2,  3,  4]
                    [ 5,  6,  7,  8]
                    [ 9, 10, 11, 12]

Input:  @integers = (1, 2, 3), $rows = 1, $cols = 3
Output: @matrix =   [1, 2, 3]

Input:  @integers = (1, 2, 3, 4), $rows = 4, $cols = 1
Output: @matrix =   [1]
                    [2]
                    [3]
                    [4]

Input:  @integers = (642, 478, 674, 906, 462, 835, 810,
352, 136, 208, 354, 459, 101, 598, 685, 632, 597, 113,
875, 490, 274, 770, 727, 536, 231, 130, 694, 604, 267,
724, 925, 712, 226, 62, 204, 214, 598, 269, 798, 862, 491,
821, 703, 306, 837, 989, 575, 342, 257, 529, 815, 128,
337, 118, 242, 238, 604, 439, 156, 735, 104, 700, 902,
975, 42, 383, 667, 56, 148, 996, 790, 356, 666, 462, 370,
375, 636), $rows = 11, $cols = 7
Output: @matrix =   [642, 478, 674, 906, 462, 835, 810]
                    [352, 136, 208, 354, 459, 101, 598]
                    [685, 632, 597, 113, 875, 490, 274]
                    [770, 727, 536, 231, 130, 694, 604]
                    [267, 724, 925, 712, 226,  62, 204]
                    [214, 598, 269, 798, 862, 491, 821]
                    [703, 306, 837, 989, 575, 342, 257]
                    [529, 815, 128, 337, 118, 242, 238]
                    [604, 439, 156, 735, 104, 700, 902]
                    [975,  42, 383, 667,  56, 148, 996]
                    [790, 356, 666, 462, 370, 375, 636]

 

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