Here is a solution to the problem:
# read in matrix while (<DATA>) { next if /^\s*$/; push @r, [split]; } # find the boundaries for my $r (0..@r-1) { for my $c (0..@{$r[0]}-2) { $vert[$r][$c] = $r[$r][$c] ne $r[$r][$c+1] ? 1 : 0; } } for my $r (0..@r-2) { for my $c (0..@{$r[0]}-1) { $horz[$r][$c] = $r[$r][$c] ne $r[$r+1][$c] ? 1 : 0; } } for my $r (0..@r-2) { for my $c (0..@{$r[0]}-2) { $cross[$r][$c] = $vert[$r][$c] + $vert[$r+1][$c] + $horz[$r][$c] + $horz[$r][$c+1] > 1 ? 1 : 0; } } # Print it out for my $r (0..@r-1) { for my $c (0..@{$r[0]}-1) { print $r[$r][$c]; print $vert[$r][$c] ? '|' : ' ' if $c < @{$r[0]}-1; } print "\n"; next unless $r < @r-1; for my $c (0..@{$r[0]}-1) { print $horz[$r][$c] ? '-' : ' ' ; print $cross[$r][$c] ? '+' : ' ' if $c < @{$r[0]}-1; } print "\n"; } __DATA__ P P B R B P R B B B B R R R B B R R R B B G G B B
I have simplified the printing to just put a cross at all intersections, you can easily generalize to vertical and horizontal connections, etc. I'll leave the conversion to Fortran 1-based matrices to you :-)

Update: If efficiency is a concern, the three passes to find the boundary can be combined into one with a little extra logic. For large matrices, this change may blow the cache fewer times.

-Mark


In reply to Re: Map grid to boundary conversion algorithm by kvale
in thread Map grid to boundary conversion algorithm by Willard B. Trophy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.