#!/usr/bin/perl use strict; use warnings; my $string = '2raccoon49-racing'; my @tests = qw(Winnie Pooh racat Bats xyz35 Barack ccoons acc 2ra); print "\n\nOur search string is : \"$string\"\n\nMinimum number of letters that must match.\n|\n|\nV"; foreach (@tests) { print "\t$_"; } for (my $N = 1; $N < 7; $N++) { print "\n$N"; foreach (@tests) { print "\t", PartialMatchStr($string, $_, $N); } } print "\n\n"; ################################################## # This function performs a partial match and returns 1 # if at least N bytes of string1 and string2 match # anywhere. Returns zero otherwise. # # Usage: INTEGER = PartialMatchStr(STR1, STR2, N) # sub PartialMatchStr { @_ > 2 or return 0; foreach (@_) { defined $_ or return 0; } my $N = $_[2]; my $L = length($_[1]) - $N; if (length($_[0]) < $N || $L < 0) { return 0; } # Impossible for (my $i = 0; $i <= $L; $i++) { # Take one segment from STR1 and find it within STR2. if ( index($_[0], substr($_[1], $i, $N) ) >= 0) { return 1; } } return 0; } ##################################################