in reply to Arrays of Arrays

Here's a version of the solution seen before using map but modified to cope with an odd number of elements in the original array. It also uses a Perl-style rather than a C-style loop to do the printing.

use strict; use warnings; my @array = ( 1 .. 9 ); my @newArray = map { [ exists $array[ 2 * $_ + 1 ] ? ( $array[ 2 * $_ ], $array[ 2 * $_ + 1 ] ) : $array[ 2 * $_ ] ] } 0 .. $#array / 2; foreach my $raSubArray ( @newArray ) { local $" = q{, }; print qq{[ @$raSubArray ]\n}; }

Here's the output.

[ 1, 2 ] [ 3, 4 ] [ 5, 6 ] [ 7, 8 ] [ 9 ]

I hope this is useful.

Cheers,

JohnGG

Replies are listed 'Best First'.
Re^2: Arrays of Arrays
by perl_junior_121 (Initiate) on Nov 07, 2008 at 12:55 UTC
      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

      sorry for repeated posting but I am new to this forum and the question format changed after posting it. I thought to repost it again. Sorry. Here's the output of your script John. 1, 2 3, 4 5, 67, 89. but can you pls explain how do i control the array size? for example: EX1: 123 in an array, 456 in an array and 789 in an array Ex2: 1234 in an array, 5678 in an array.... 1,2,34,5,67,8,9 or 1,2,3,4 5,6,7,8 9 also can anybody let me know if there is any good documentation for dummies to learn subroutines. Thanks
        Hi john, Thanks for your kind help. I could learn how to get the required matrix dimensions playing around with your code. But it would be great if you can explain me a bit about the code on your convenience. Thank you very much. Enjoy your weekend! cheers, junior
Re^2: Arrays of Arrays
by perl_junior_121 (Initiate) on Nov 07, 2008 at 12:47 UTC
    Hi John, The solution you gave was what i needed. Thank you very much. Can you please help me understand how it works. Thanks. cheers, Junior