Here is my stab at the problem. Basically we pad the image with spaces to make a rectangular image. We do the same with the subimage but keeping the width of the subimage the same as the width of the image (padding with spaces). Create a regex by matching anything in space or the same character otherwise. Print the row and column of where the match occured
#!/usr/bin/perl -w
use strict;
my $image = <<'EOF';
MM
oo
<
-/
/^\
: C :
8===8
|^|
|||
-~-~
EOF
my $subimage = <<'EOF';
/^\
C
EOF
matches($image, $subimage);
sub matches{
my ($img, $simg) = @_;
my $len = 0;
my $normalize = sub {
my @arr = split /\n/, shift;
for(map length, @arr){
$len = $_ if $len < $_;
}
join "", map {$_ . ' ' x ($len - length)} @arr;
};
$img = $normalize->($img);
$simg = quotemeta $normalize->($simg);
$simg =~ s/\\ /./g;
if($img =~ /(.*?)$simg/){
my $l = length $1;
my $row = int($l / $len);
my $col = $l % $len;
print "Matches at $row x $col\n";
} else {
print "No match\n";
}
}
# Matches at 4 x 1
The row and column start with zero.
Hope this helps,,,