Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

If you insist in using a range-/list-op, you could do:

if ( grep { $ref == $_ } 1..8 ) { ... }
But personally, I would rather use something like
if ( $ref >= 1 and $ref <= 8 ) { ... }
or maybe
if ( $ref =~ /^[1-8]$/ ) { ... }

Update: (in response to OP's reply below) With a modern Perl, you could use given/when:

use v5.10; use strict; use warnings; my @tests = qw(1 9 17 25 33); my $range_25_32 = [25..32]; # alternatve, maybe(?) faster for my $ref ( @tests ) { given ( $ref ) { when ( [1..8] ) { say "A $ref" } when ( [9..16] ) { say "B $ref" } when ( [17..24] ) { say "C $ref" } when ( $range_25_32 ) { say "D $ref" } default { say "ELSE $ref" } } }
Output:
A 1 B 9 C 17 D 25 ELSE 33
With the given ranges in the example below, some bitwise operators might also work.

In reply to Re: Range Operator in If Statement by Perlbotics
in thread Range Operator in If Statement by mmartin

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 taking refuge in the Monastery: (7)
As of 2024-04-23 13:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found