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.
- $#array is the highest subscript of @array and the 0 .. $#array / 2 uses the range operator (..) to construct a list of numbers from zero up to halfway along the array (the division will truncate since the range operator expects whole numbers.
- The list is passed into the map one at a time in the scalar variable $_.
- Inside the map we use the anonymous array constructor ([ ... ]) to make an array reference that will be passed out and added to @newArray each time through.
- The code inside the array constructor uses a ternary (see Conditional Operator in perlop) to test whether we are dealing with two elements or one; with two, the condition succeeds and the first statement making a list of two elements is executed; if this is the last time through with an odd number of elements in the original array, the test will fail and the second statement just passing the one element we have is done. Thus, each anonymous array passed from the map by reference will contain usually two (and possibly one for the last) elements.
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.
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 |