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,,,
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, 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, 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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|