#!/usr/bin/perl -w use strict; use List::MoreUtils qw{uniq}; my $sample = "A-E-H-L"; my @options = ( "A-B-F-G-H-K-M", # idx = 0 "A-E-G-H-L", # idx = 1 "A-C-E-G-H-J-L", # idx = 2 "B-F-H-K", # idx = 3 "A-B-F-G-H-K-L", # idx = 4 "C-H" , # idx = 5 "A-E-H-L", # idx = 6 "H-E-A-L" # idx = 7 ); my @indicies = find_indexes (\@options, $sample); print "sample $sample occurs in indicies: @indicies\n"; sub find_indexes { my ($aref, $template) = @_; my @indicies; my @patterns = $template =~ /\w+/g; my $regex = join ("|", @patterns); # "OR" is easy with regex # "AND" is hard my $index; foreach my $line (@$aref) { my (@matches) = $line =~ /$regex/g; push @indicies, $index if (uniq (@matches) >= @patterns); $index++; } return @indicies; } __END__ Prints: sample A-E-H-L occurs in indicies: 1 2 6 7
In reply to Re: Pattern Matching with a Selected Sub-String
by Marshall
in thread Pattern Matching with a Selected Sub-String
by ozboomer
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |