Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I'm trying to do the following:
if ( ($item1 or $item2) eq ($item3) ) { #do something; }

Just use a junction. Oh no, Perl 5 doesn't have junctions. Well, then there's Quantum::Superpositions, but personally I wouldn't use it...

In fact I've had to workaround:
my $result = grep { $_ eq $item3 } ($item1, $item2); if ($result) { #do something }

That's fine enough, has the advantage of having a straightforward generalization to more items and can be cast into a IMHO simpler form like thus:

if (grep $_ eq $item3, $item1, $item2) { #do something }
Is there a more direct statement one can use?

More direct?

if ($item1 eq $item3 or $item2 eq $item3) { #do something }

This has the advantage of or's short circuiting, but in this case it really seems like a micro-optimization not worth the effort. To generalize to an arbitrary number of items you'd better need a hyperoperator that's not in Perl 5 either.


In reply to Re: testing more than one item for equality by blazar
in thread testing more than one item for equality by jonnyfolk

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 romping around the Monastery: (2)
As of 2024-04-25 19:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found