in reply to Re^4: Comparing 2 different-sized strings
in thread Comparing 2 different-sized strings
could you just explain what's going on inside the map function please?
Sure.
sub fuzzyMatch { my( $rHay, $rNee, $misses ) = @_; my $lNee = length $$rNee; my $min = $lNee - $misses; map { ( ( substr( $$rHay, $_, $lNee ) ^ $$rNee ) =~ tr[\0][] ) >= $min ? $_ : () } 0 .. length( $$rHay ) - $lNee; }
Hence the map counter runs from 0 to length( haystack) - length( needle ).
Hence, the substr presents a needle length substring of haystack at each of those counter positions.
So we bit-wise xor (^) the substring and the needle.
The result is a string that has a 0 (null) byte wherever the two strings match; and some other byte value where they do not.
tr[\0][] does that efficiently.
return the position where the match occurred ($_), otherwise return nothing (()).
Hope that clarifies things little. Continue to ask about anything that isn't clear.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: Comparing 2 different-sized strings
by AdrianJ217 (Novice) on Aug 11, 2013 at 08:43 UTC | |
by BrowserUk (Patriarch) on Aug 11, 2013 at 09:12 UTC | |
by AdrianJ217 (Novice) on Aug 12, 2013 at 14:08 UTC | |
by BrowserUk (Patriarch) on Aug 12, 2013 at 14:41 UTC | |
by Anonymous Monk on Aug 18, 2013 at 13:20 UTC | |
| |
by AdrianJ217 (Novice) on Aug 12, 2013 at 22:44 UTC | |
by BrowserUk (Patriarch) on Aug 12, 2013 at 22:54 UTC | |
|