in reply to Re^2: Searching parallel arrays.
in thread Searching parallel arrays.

I'v tried to code it, without reading codes posted previously, just for enjoy. I'v been curious, how many rows it will has. O.K., it is not very perlish... Idea was very simple - zip the four arrays to one stream, fill circle buffer and test if last value of the buffer - 3 is equal to first value. Damn, 67 rows.
use strict; use warnings; my @a1 = ( 100, 204, 208, 312 ); my @a2 = ( 102, 313, 409 ); my @a3 = ( 205, 206, 315 ); my @a4 = ( 207, 210, 314 ); my $a = [\@a1,\@a2,\@a3,\@a4]; my @ai = (0, 0, 0, 0); my ($a1a2, $a3a4) = (getLess(0), getLess(2)); my @circle = (getNext(),getNext(),getNext()); my $ci = 3; while (defined($circle[$ci] = getNext())) { if ($circle[$ci][2]-3 == $circle[($ci+1)%4][2]) { for (my $i = 1; $i < 5; $i++) { print $circle[($ci+$i)%4][2]. ' ['.$circle[($ci+$i)%4][0].','.$circle[($ci+$i)%4][1].'] '; } print "\n"; } $ci = ($ci+1)%4; } sub getLess { my $offset = shift; my $ret; if (defined $$a[$offset][$ai[$offset]]) { if (defined $$a[$offset+1][$ai[$offset+1]]) { $offset++ if $$a[$offset][$ai[$offset]] > $$a[$offset+1][$ai[$ +offset+1]]; } $ret = [$offset, $ai[$offset], $$a[$offset][$ai[$offset]]]; $ai[$offset]++; } else { $offset++; if (defined $$a[$offset][$ai[$offset]]) { $ret = [$offset, $ai[$offset], $$a[$offset][$ai[$offset]]]; $ai[$offset]++; } } return $ret; } sub getNext { my $ret; if (defined $a1a2) { if (defined $a3a4) { if ($$a1a2[2] < $$a3a4[2]) { $ret = $a1a2; $a1a2 = getLess(0); } else { $ret = $a3a4; $a3a4 = getLess(2); } } else { $ret = $a1a2; $a1a2 = getLess(0); } } else { if (defined $a3a4) { $ret = $a3a4; $a3a4 = getLess(2); } } return $ret; }
updated: output is
204 [0,1] 205 [2,0] 206 [2,1] 207 [3,0] 205 [2,0] 206 [2,1] 207 [3,0] 208 [0,2] 312 [0,3] 313 [1,1] 314 [3,2] 315 [2,2]