Hi Junior,

Sorry for the delay in replying to your supplementary questions, real $work intervened. Have a quick look at Writeup Formatting Tips to see how you can improve the look of your posts. Specifically, the site uses square brackets to introduce mark-up elements which is why your arrays look like links. Surround stuff like that with <code> and </code> or <c> and </c> tags; don't use <pre> tags.

Going back to the code I posted, have a look at map to see what it does. Here are the stages broken down.

To extend the method for different sizes of sub-arrays we need to slightly change the way we test for the last iteration and emit array slices (see Slices in perldata). Here is a more general version with the partitioning and printing moved into subroutines.

use strict; use warnings; my @array = map { sprintf q{%02d}, $_ } 1 .. 17; print q{=} x 25, qq{\n}; for ( 1 .. 10 ) { print qq{Partition size = $_\n}; my @newArray = partitionArray( $_, @array ); printAoA( @newArray ); } sub partitionArray { my( $ps, @array ) = @_; my @newArray = map { [ exists $array[ $ps * $_ + $ps - 1 ] ? @array[ ( $ps * $_ ).. ( $ps * $_ + $ps - 1 ) ] : @array[ ( $ps * $_ ) .. $#array] ] } 0 .. $#array / $ps; } sub printAoA { foreach my $raSubArray ( @_ ) { local $" = q{, }; print qq{[ @$raSubArray ]\n}; } print q{=} x 25, qq{\n}; }

The output.

========================= Partition size = 1 [ 01 ] [ 02 ] [ 03 ] [ 04 ] [ 05 ] [ 06 ] [ 07 ] [ 08 ] [ 09 ] [ 10 ] [ 11 ] [ 12 ] [ 13 ] [ 14 ] [ 15 ] [ 16 ] [ 17 ] ========================= Partition size = 2 [ 01, 02 ] [ 03, 04 ] [ 05, 06 ] [ 07, 08 ] [ 09, 10 ] [ 11, 12 ] [ 13, 14 ] [ 15, 16 ] [ 17 ] ========================= Partition size = 3 [ 01, 02, 03 ] [ 04, 05, 06 ] [ 07, 08, 09 ] [ 10, 11, 12 ] [ 13, 14, 15 ] [ 16, 17 ] ========================= Partition size = 4 [ 01, 02, 03, 04 ] [ 05, 06, 07, 08 ] [ 09, 10, 11, 12 ] [ 13, 14, 15, 16 ] [ 17 ] ========================= Partition size = 5 [ 01, 02, 03, 04, 05 ] [ 06, 07, 08, 09, 10 ] [ 11, 12, 13, 14, 15 ] [ 16, 17 ] ========================= Partition size = 6 [ 01, 02, 03, 04, 05, 06 ] [ 07, 08, 09, 10, 11, 12 ] [ 13, 14, 15, 16, 17 ] ========================= Partition size = 7 [ 01, 02, 03, 04, 05, 06, 07 ] [ 08, 09, 10, 11, 12, 13, 14 ] [ 15, 16, 17 ] ========================= Partition size = 8 [ 01, 02, 03, 04, 05, 06, 07, 08 ] [ 09, 10, 11, 12, 13, 14, 15, 16 ] [ 17 ] ========================= Partition size = 9 [ 01, 02, 03, 04, 05, 06, 07, 08, 09 ] [ 10, 11, 12, 13, 14, 15, 16, 17 ] ========================= Partition size = 10 [ 01, 02, 03, 04, 05, 06, 07, 08, 09, 10 ] [ 11, 12, 13, 14, 15, 16, 17 ] =========================

A simpler method might be to use splice to remove successive chunks of elements from the front of the array while the number of elements left is greater than the partition size, finally pushing a reference to what is left as our last sub-array.

use strict; use warnings; my @array = map { sprintf q{%02d}, $_ } 1 .. 17; print q{=} x 25, qq{\n}; for ( 1 .. 10 ) { print qq{Partition size = $_\n}; my @newArray = partitionArray( $_, @array ); printAoA( @newArray ); } sub partitionArray { my( $partSize, @array ) = @_; my @partitioned = (); while( scalar @array > $partSize ) { push @partitioned, [ splice @array, 0, $partSize ]; } push @partitioned, \ @array; return @partitioned; } sub printAoA { foreach my $raSubArray ( @_ ) { local $" = q{, }; print qq{[ @$raSubArray ]\n}; } print q{=} x 25, qq{\n}; }

The output is identical.

I hope you find this helpful. If you have any more questions please don't hesitate to ask.

Cheers,

JohnGG


In reply to Re^3: Arrays of Arrays by johngg
in thread Arrays of Arrays by perl_junior_121

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.