in reply to Partial Searches Against Multiple Wildcards

Just an idea

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11156323 use warnings; my %data = map { (split)[1], $_ } <DATA>; for my $searchfor ( qw( 940 ABFD-1 ABFD1 D7RD DD482 ) ) { print "\n$searchfor\n"; print ' ' x 8, $data{$_} for grep match($searchfor, $_), sort keys +%data; } sub match { my ($match, $to) = map tr/-//dr, @_; $to =~ /\*/ or return $to =~ $match; my @pattern = map s/\*/[A-Z0-9]/gr, $to =~ /(?=(.{@{[length $match]} +}))/g; local $" = '|'; return $match =~ /@pattern/; } __DATA__ 1 ABFD-1234 AAA 2 ABFD-178G BBB 3 F2HB-9401 AAA 4 ZDDR-00W5 DDD 5 D7*D-48*6 EEE

Outputs:

940 3 F2HB-9401 AAA ABFD-1 1 ABFD-1234 AAA 2 ABFD-178G BBB ABFD1 1 ABFD-1234 AAA 2 ABFD-178G BBB D7RD 5 D7*D-48*6 EEE DD482 5 D7*D-48*6 EEE

Replies are listed 'Best First'.
Re^2: Partial Searches Against Multiple Wildcards
by LanX (Saint) on Dec 18, 2023 at 21:49 UTC
    Apart from the usual "better magic than readable style", nice approach.

    You might want to consider to memoize the compiled regexes based on length($match)

    This should be considerably faster on the long run.

    OTOH it also depends on the proportion of wildcard entries.

    If it's only a small percentage your solution should be faster than hv's

    Of course he could also distinguish between those entries with wildcards and those without.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery