in reply to Partial Searches Against Multiple Wildcards

OK, so it was an interesting problem:

use strict; use warnings; use DBI; my $dbfile = 'D:\Delme~~\demo.db'; unlink $dbfile; my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile","",""); $dbh->do (qq{CREATE TABLE Demo (rowid INTEGER, old VARCHAR(9), new VAR +CHAR(3))}); my @testData = ( ['ABFD-1234' => 'AAA'], ['ABFD-178G' => 'BBB'], ['F2HB-9401' => 'AAA'], ['ZDDR-00W5' => 'DDD'], ['D7*D-48*6' => 'EEE'], ['*7RD-4896' => 'FFF'], ['D7RD-489*' => 'GGG'], ); my @old = map {$_->[0]} @testData; my @new = map {$_->[1]} @testData; my $sth = $dbh->prepare (qq{INSERT INTO Demo (old, new) VALUES (?, ?)} +); my $entries = $sth->execute_array ({}, \@old, \@new); $sth = $dbh->prepare (qq{SELECT old, new FROM Demo WHERE old LIKE ?}); $sth->execute ('%*%'); my @candidates = @{$sth->fetchall_arrayref ()}; for my $target ('D7PD', 'D7RD', '4897', '4876') { for my $candidate (map {$_->[0]} @candidates) { my @parts = split '\*', $candidate; my @rParts = reverse map {scalar reverse $_} @parts; my $str = $target; my $rStr = reverse $target; eval { matchPart($str, $_) || return for @parts; return !length $str; } or eval { $str = ''; rMatchPart($rStr, $_) || return for @rParts; return ! length $rStr; } or next; print "Matched '$target' against '$candidate'\n" if !length $s +tr; } } sub matchPart { my ($str, $match) = @_; $match = substr $match, 0, length $_[0]; return 1 if $_[0] =~ s/$match.?//; } sub rMatchPart { my ($str, $match) = @_; $match = substr $match, 0, length($str) - 1 if length $str; return 1 if $_[0] =~ s/.?$match//; }

Prints:

Matched 'D7PD' against 'D7*D-48*6' Matched 'D7RD' against 'D7*D-48*6' Matched 'D7RD' against '*7RD-4896' Matched 'D7RD' against 'D7RD-489*' Matched '4897' against 'D7RD-489*'

Only marginally tested. There are lots of edge cases that may be problematic, but maybe it's a hint in a useful direction.

Update: removed an edge case.

Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

Replies are listed 'Best First'.
Re^2: Partial Searches Against Multiple Wildcards
by p_jacobson (Novice) on Dec 18, 2023 at 04:37 UTC

    Thank you for the example code. It was an interesting solution that got me moving in the right direction. It works well when the search string matches from the beginning of (or all the way to the end of) the database string. I'll probably use hv's solution for this particular project but I think I'll try and flesh out your solution a bit more so I have another option if needed. Thanks again for taking a look.