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

Dear Perl monks,
I need to split the following comma separated text and print it in 2x2 size.
e.g.
Input:
3, 4, 10, 2 2, 7, 12, 3 0, 3, 4, 5 6, 5, 9, 11
Output:
1: 2: 3, 4 10, 2 2, 7 2, 3 3: 4: 0, 3 4, 5 6, 5 9, 11
Please help me in completing my code.
my present code:
@matrix = ([3, 4, 10, 2], [2, 7, 12, 3], [0, 3, 4, 5], [6, 5, 9, 11]); for($row = 0; $row < 2; $row++) { for($col = 0; $col < 2; $col++) { print "$matrix[$row][$col] "; } print "\n"; }
Thanks in advance.

Replies are listed 'Best First'.
Re: for loop
by psini (Deacon) on Sep 23, 2008 at 10:15 UTC

    This problem looks like homework, but...

    I'd start adding use strict; use warnings; at the begin of your code, then making all your vars lexical (my).

    Your code prints a single block from the input matrix, you obviously could replicate the code for each of the blocks, but it is not the best way. Perhaps you should parametrize your loops e.g. for(my $row = 2*$blockRow; $row < 2*$blockRow+2; $row++)

    So you have a code, that you could put in a sub, that can print any of the blocks you require; then another couple of for loops can be used to print all the blocks.

    Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."

Re: for loop
by apl (Monsignor) on Sep 23, 2008 at 09:59 UTC
    When you run your program, what are the results you get? Not what you want, right? What do you have to change to get what you want?

    In otherwords, try doing that problem by hand. Write some pseudo-code to implement your solution. Then translate that into Perl.

Re: for loop
by ikegami (Patriarch) on Sep 23, 2008 at 19:43 UTC
    Insert extra spaces every two columns, and to insert a new line every two rows. The modulus operator (%) will help you determine when the time is right.
Re: for loop
by svenXY (Deacon) on Sep 23, 2008 at 10:02 UTC
    Hi,
    smells a lot like homework...
    Regards,
    svenXY
Re: for loop
by dwm042 (Priest) on Sep 23, 2008 at 18:36 UTC
    This is one way to handle this problem. It is tied to the exact layout of @matrix, so beware:

    #!/usr/bin/perl use warnings; use strict; my @matrix = ([3, 4, 10, 2], [2, 7, 12, 3], [0, 3, 4, 5], [6, 5, 9, 11]); print_2by2(\@matrix); sub print_2by2 { my $aref = shift; my @copy = @{$aref}; my $length = scalar @matrix; my $count = 1; while ( $length > 0 ) { $length -= 2; my $rowref1 = shift @copy; my $rowref2 = shift @copy; my $rowlength = scalar @{$rowref1}; for ( my $j = 0; $j < $rowlength/2; $j++ ) { my $index = $j + $count; print $index, ":"; print " "; } print "\n"; $count += $rowlength/2; rowprint($rowref1); rowprint($rowref2); print "\n"; } } sub rowprint { my $rowref = shift; my $rowlength = scalar @{$rowref}; for ( my $i = 0; $i < $rowlength; $i++ ) { print $$rowref[$i]; if ( $i % 2 == 0 ) { print ", "; } else { print " "; } } print "\n"; }
    The output is:

    C:\Code>perl fancy_print.pl 1: 2: 3, 4 10, 2 2, 7 12, 3 3: 4: 0, 3 4, 5 6, 5 9, 11
Re: for loop
by indragoby (Initiate) on Sep 29, 2008 at 09:22 UTC
    Hi, My understanding is of the above problem is to split the matrix to an expected order. And my code is:

    sub splitMatrix {
    my $val = shift;
    my $order = shift;
    my ($i, $i1, $j, $j1, $k, $k1, $orgRowOrder, $orgColOrder, @retval);
    $orgRowOrder = scalar(@{$val});
    $orgColOrder = scalar(@{$$val[0] });
    for ( $i = 0, $j = 0; $j < $orgRowOrder; $j++ ) {
    $i1 = $i;
    for ($k = 0 ; $k < $orgColOrder; $k++) {
    $j1 = $j % $order;
    $k1 = $k% $order;
    $retval[$i1] [$j1] [$k1] = $$val[$j] [$k] ;
    $i1++ if ( $k % $order == $order-1 );
    $j1++ if ( $i1 % $order == $order-1 );
    $k1++;
    }
    $i += $order+1 if ( $j % $order == $order-1);
    }
    return @retval;
    }

    my @matrix = ([3, 4, 10, 2,2,2] ,
    [2, 7, 12, 3,2,2] ,
    [0, 3, 4, 5,2,2] ,
    [6, 5, 9, 11,2,2] );

    my ($a,$b,$c,$d,$e,$f);
    ($a,$b,$c,$d, $e, $f) = &splitMatrix(\@matrix,2);


    print "\n1: ";
    foreach my $temp ( @$a ) {
    foreach my $temp2 ( @$temp ) {
    print $temp2 ,"|\t";
    }
    print "\n ";
    }

    print "\n2: ";

    foreach my $temp ( @$b ) {
    foreach my $temp2 ( @$temp ) {
    print $temp2 ,"|\t";
    }
    print "\n ";
    }


    email: indragoby@yahoo.com