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.
-
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.