Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Pattern Matching with a Selected Sub-String

by Marshall (Prior)
on Jul 27, 2012 at 12:34 UTC ( #984019=note: print w/ replies, xml ) Need Help??


in reply to Pattern Matching with a Selected Sub-String

Here is another way to do this...

#!/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


Comment on Re: Pattern Matching with a Selected Sub-String
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://984019]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2014-09-23 11:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (219 votes), past polls