Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Comparing multiple strings

by Eily (Monsignor)
on Jan 15, 2019 at 16:50 UTC ( [id://1228602]=note: print w/replies, xml ) Need Help??


in reply to Comparing multiple strings

One way would be to put all the strings you want to match (or negative match) in a hash so that you can check for their existance:

my @invalid_list = qw< John Simon Mathias Aerith Bob >; my @test = qw< Alice Bob John Doe >; my %invalid = map { $_ => 1 } @invalid_list; for my $name (@test) { print "$name\n" unless exists $invalid{$name}; }

Or, as proposed by tobyink, you can use List::Util:

use List::Util qw( none all any ); for my $name (@test) { print "None > $name\n" if none { $name eq $_ } @invalid_list; print "Any > $name\n" unless any { $name eq $_ } @invalid_list; print "All > $name\n" if all { $name ne $_ } @invalid_list; }
(There's also notall, but it would have required a triple negation in this case).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-04-18 18:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found