Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Pattern Matching with a Selected Sub-String

by Marshall (Canon)
on Jul 27, 2012 at 12:34 UTC ( [id://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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2024-04-20 03:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found