aboutsummaryrefslogtreecommitdiff
path: root/challenge-078/sgreen/perl/ch-2.pl
blob: 7e458d013d91c63d91f5b0dd08e6b1aea5a8f7ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env perl

use strict;
use warnings;
use feature qw(say);

sub main {
    my ( $array, $shift ) = @_;

    # Split the values into an array
    my @array   = ( $array =~ /(\d+(?:\.\d+)?)/gm );
    my @shift   = ( $shift =~ /(\d+)/gm );
    my @results = ();

    foreach my $offset (@shift) {
        if ( $offset > $#array ) {
            die "$offset is >= the size of the array\n";
        }

        if ( $offset == 0 ) {
            push @results, [@array];
        }
        else {
            push @results, [ @array[ $offset .. $#array, 0 .. $offset - 1 ] ];
        }
    }

    foreach my $result (@results) {
        say '[', join( ' ', @$result ), ']';
    }
}

main(@ARGV);