use feature qw(say); my @widths = (2, 6, 5, 7); foreach (1 .. 2) { my @partitions = do { my $c = 0; map { $c += $_; [$c - $_, $c] } @widths}; say '[', join(', ', @$_), ']' for @partitions; print "\n"; }