Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: ensure ALL patterns in string

by jeffa (Bishop)
on Jan 22, 2016 at 20:55 UTC ( #1153414=note: print w/replies, xml ) Need Help??


in reply to ensure ALL patterns in string

There are few ways you can condense this, here is one:

print "all in m8\n" if $var =~ /.*x.*y.*z.*/;
As in:
for (<DATA>) { chomp; print "$_: all in m8\n" if /.*x.*y.*z.*/; } __DATA__ foo xoo fyo foz xyo fyz xyz

UPDATE: Nope, that won't capture 'zyx' (and .* is awful) ... you could mangle the regex more or ... just use a lookup table:

my %hash = ( x => 1, y => 1, z => 1 ); for (<DATA>) { chomp; my $in = 0; my %hash = ( x => 1, y => 1, z => 1 ); for my $x (split //, $_) { delete $hash{$x} and $in++; } print "$_: all in m8\n" if $in == 3; }
That works, but perhaps List::Util is the way to go ... yes, i think so. :)

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Re^2: ensure ALL patterns in string
by previous (Sexton) on Jan 22, 2016 at 21:47 UTC
    Thanks for the alternatives. I'm quite new to Perl and it's very educational to see the various methods. Thank you.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2019-05-20 19:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you enjoy 3D movies?



    Results (128 votes). Check out past polls.

    Notices?