<?xml version="1.0" encoding="windows-1252"?>
<node id="341489" title="Iterating over Blocks of 2-Dim Array" created="2004-03-31 21:49:44" updated="2005-02-15 11:15:37">
<type id="115">
perlquestion</type>
<author id="6040">
Kozz</author>
<data>
<field name="doctext">
&lt;p&gt;Esteemed monks:&lt;/p&gt;

&lt;p&gt;I've got data in an n-by-n two-dimensional array.  Assuming that my n is divisible by two, I need to be able to iterate over the items in sub-matrices.  Let me explain, and see if you can help me with this algorithm.  I've tried, but can't quite wrap my head around it, and the code I DO make always tends to be far uglier than I'd expect it to be.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Disclosure:&lt;/b&gt; This &lt;em&gt;is&lt;/em&gt; homework related.  However, iterating over the data in this manner is not the main thrust of it.  My "work" was done in the assignment of the 2-dim array elements' values.  I just chose to break up the output into more manageable blocks (perhaps badly?).&lt;/p&gt;
&lt;readmore&gt;
&lt;p&gt;Supposing n = 4, then I've got 4 "rows" of indices 0..3, each row having a "column" with indices 0..3, like this (vertical bars and dashed line to represent a line dividing matrix into quadrants):
&lt;code&gt;
[ 00 01 | 02 03 ]
[ 10 11 | 12 13 ]
-----------------
[ 20 21 | 22 23 ]
[ 30 31 | 32 33 ]
&lt;/code&gt;
I want to output the four elements in the upper-left quadrant of the matrix (00, 01, 10, 11, in that order), then upper right, lower left, lower right.
&lt;/p&gt;
&lt;p&gt;What trips me up is that I iterate over the first two cols of the first row, then increment row but reset cols.  Then row gets reset and cols adjusted.... it's just a very awkward traversal of the matrix.&lt;/p&gt;
&lt;/readmore&gt;
&lt;p&gt;I know this is more of an algorithms question than a perl question, so smack me down with a &lt;b&gt;--&lt;/b&gt; if you must.  But I know that there are monks that can likely whip up a very elegant solution for this.&lt;/p&gt;
&lt;p&gt;Thanks for any insight you can provide.&lt;/p&gt;
&lt;readmore&gt;
&lt;p&gt;
&lt;b&gt;Update:&lt;/b&gt; Code per request.  This script was made just for a "simple" (ha-ha) base case to see if I could figure out how to iterate over pairs of indices in this manner.  I know I've got an infinite loop, too, while I'm trying to figure out when to increment/decrement my rows &amp; columns.
&lt;code&gt;
#!/usr/bin/perl

my $n = 2;
my $max_exp = 1;

use strict;

my $max_part = 2**$max_exp;

my $size = 2**$n;

print "Matrix is $size by $size.\n";
print "Max size I'm allowed to print is $max_part at a time.\n";

my $max_part_tiles = $max_part**2;

my $total_tiles = ($size)**2;
my $tiles_counted = 0;
my $part_tiles_counted = 0;

my ($row_num, $col_num, $end_col) = (0, 0);
my ($cols_displayed, $rows_displayed) = (0, 0);

while($tiles_counted &lt; $total_tiles){

        $col_num=0;
        $end_col=$max_part;
        while($row_num &lt; $size){
                while($col_num &lt; $end_col){
                        print "$row_num $col_num\n";
                        $col_num++;
                        $part_tiles_counted++;
                }
                if($col_num &lt; ($size-1) &amp;&amp; (($row_num + 1) % $size &gt; 0) ){
                        $row_num++;
                        $col_num-=$max_part;
                }
                if($col_num &lt; ($max_part-1) &amp;&amp; $row_num == ($size-1)){
                        # $col_num++;
                        $row_num-=$max_part;
                        $end_col+=$max_part;
                }
                if($col_num == ($size-1) &amp;&amp; $row_num &lt; ($size-1)){
                        $row_num++;
                        $col_num-=$max_part;
                }
                if($col_num == ($max_part-1) &amp;&amp; $row_num == ($size-1)){
                        $row_num++;
                        $col_num=0;
                        $end_col=$max_part;
                }
                $tiles_counted += $part_tiles_counted;
        }

}
&lt;/code&gt;
&lt;/readmore&gt;</field>
</data>
</node>
