perl_junior_121 has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
If there is an array with many elements in it, how do i create arrays of arrays with 2 elements in each array. I would appreciate if anybody can help me explain doing this. thanks in advance.
@array =("1","2","3","4","5","6","7","8");
The output I am looking for is
@new_array =(["1", "2"], ["3","4"],["5","6"],["7","8"]);
My code:
@array =("1","2","3","4","5","6","7","8"); for($x=0;$x<=$#array;$x++){ push @new_array, $array[$x]; { print "@new_array\n";
This prints the entire array but will be thankful if anyone of you can explain me how to push/store 2 elements of each in a @new_array( 1 and 2 as a array, 3 and 4 as a array...) i.e the new array should contain:
@new_array =(["1", "2"], ["3","4"],["5","6"],["7","8"]);
Thanks,
Junior

Replies are listed 'Best First'.
Re: Arrays of Arrays
by johngg (Canon) on Nov 07, 2008 at 12:33 UTC
    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

        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, The solution you gave was what i needed. Thank you very much. Can you please help me understand how it works. Thanks. cheers, Junior
Re: Arrays of Arrays
by AnomalousMonk (Archbishop) on Nov 07, 2008 at 11:54 UTC
    You may be interested in the tutorials Multidimensional Arrays and, with respect to the use of map, Map: The Basics.

    As has been pointed out, the critical thing to keep in mind is that Perl does not have multi-dimensional arrays per se, but uses nested arrays of references to simulate arrays of arrays, arrays of hashes, etc., etc. See perldsc for more info.

      Thank you all. I will try to work on the given solutions and will inform you if it works. Thanks.
Re: Arrays of Arrays
by hda (Chaplain) on Nov 07, 2008 at 12:35 UTC
    Hi,

    If your arrays are numeric you can greatly benefit from using PDL (Perl Data Language). Just use as many dimensions as you want in a single "piddle", i.e. a PDL variable.

    Check the documentation in the PDL web page. Also there is a new version coming up!

    Cheers
Re: Arrays of Arrays
by ccn (Vicar) on Nov 07, 2008 at 11:17 UTC
    my @array = (1, 2, 3, 4, 5, 6, 7, 8); my @new_array = map{ [$array[2 * $_ - 1], $array[2 * $_]] } 1 .. @arra +y/2;
      You have an off-by-one error here.

      $ perl -MData::Dumper -e ' > my @array = (1, 2, 3, 4, 5, 6, 7, 8); > my @new_array = map{ [$array[2 * $_ - 1], $array[2 * $_]] } 1 .. @ar +ray/2; -> print Dumper(\@new_array);' $VAR1 = [ [ 2, 3 ], [ 4, 5 ], [ 6, 7 ], [ 8, undef ] ]; $

      This works.

      $ perl -MData::Dumper -e ' > my @array = (1, 2, 3, 4, 5, 6, 7, 8); > my @new_array = map{ [$array[2 * $_], $array[2 * $_ + 1]] } 0 .. $#a +rray/2; -> print Dumper(\@new_array);' $VAR1 = [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7, 8 ] ]; $

      Cheers,

      JohnGG

      $ perl -le' my @array = (1, 2, 3, 4, 5, 6, 7, 8); my @new_array = map{ [$array[2 * $_ - 1], $array[2 * $_]] } 1 .. @arra +y/2; use Data::Dumper; print Dumper \@new_array; ' $VAR1 = [ [ 2, 3 ], [ 4, 5 ], [ 6, 7 ], [ 8, undef ] ];

      This appears to work better:

      $ perl -le' my @array = 1 .. 8; my @new_array = map [ @array[ 2 * $_ - 2, 2 * $_ - 1 ] ], 1 .. @array +/ 2; use Data::Dumper; print Dumper \@new_array; ' $VAR1 = [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7, 8 ] ];
Re: Arrays of Arrays
by sanku (Beadle) on Nov 07, 2008 at 11:44 UTC
    hi see the following code.
    use strict; use warnings; my @array =(["1","2"],["3","4"],["5","6"],["7","8"]); for (my $j=0; $j<=$#{$array[0]}; $j++) { for (my $k=0; $k<=$#array; $k++) { print $array[$k]->[$j]; } print "\n"; }
    By this way you can bale to assign tow elements as an array in perl. Actually in perl two dimensional array will be created using references.