Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Looks to me like you aren't really that interested in the order of the values in @owner_grouprights and instead just want to be able to easily check whether specific values are present or not. This is not a job for an array. You want a hash:
#!/usr/bin/env perl use strict; use warnings; use 5.010; # Don't really need the qw, since these are numbers, but the original # code explicitly made them strings, so whatever. my @owner_grouprights = qw( 11 14 ); my %rights_hash; $rights_hash{$_}++ for @owner_grouprights; if (exists $rights_hash{11}) { say "test3"; } for (4 .. 12) { if (exists $rights_hash{$_}) { say "test2"; last; # avoid duplicate messages if more than one is present } } # Or, alternately: if (grep /4|5|6|7|8|9|10|11|12/, keys %rights_hash) { say "test2 alt"; } # Note, though, that the two alternatives are not equivalent. The # first (if exists) only returns exact matches, while the second # (grep) also finds substring matches. So, e.g., 114 would match the # 'grep' version, but not the 'if exists' version. Adding begin/ # end-of-string anchors (^/$ or \A/\Z) to the 'grep' version's regex # will disallow partial matches if you want that, but don't want the # explicit 'for' loop. my $owner_grouprights = join( ' ', sort keys %rights_hash); my @testarr = (4 .. 12); for my $tst (@testarr) { if (exists $rights_hash{$tst}) { say "$tst hit!"; } else { say "$tst miss"; } }

In reply to Re: smartmatch with multiple search values by dsheroh
in thread smartmatch with multiple search values by toohoo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-23 09:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found