in reply to Range Operator in If Statement
If you insist in using a range-/list-op, you could do:
But personally, I would rather use something likeif ( grep { $ref == $_ } 1..8 ) { ... }
or maybeif ( $ref >= 1 and $ref <= 8 ) { ... }
if ( $ref =~ /^[1-8]$/ ) { ... }
Update: (in response to OP's reply below) With a modern Perl, you could use given/when:
Output: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" } } }
With the given ranges in the example below, some bitwise operators might also work.A 1 B 9 C 17 D 25 ELSE 33
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Range Operator in If Statement
by mmartin (Monk) on Jan 04, 2012 at 20:55 UTC | |
by Marshall (Canon) on Jan 04, 2012 at 21:34 UTC | |
by LanX (Saint) on Jan 05, 2012 at 01:56 UTC | |
by ambrus (Abbot) on Jan 05, 2012 at 11:43 UTC |
In Section
Seekers of Perl Wisdom