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.
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |