in reply to Quicker way to do this IF statement 1-5

my @valid= 1..5; my %valid= map { $_=>1 } @valid; # ... $valid{$b} or print "\$b has to be one of ", join(", ",@valid[0..$#valid-1]), " or $valid[-1]\n";
---
$world=~s/war/peace/g

Replies are listed 'Best First'.
Re^2: Quicker way to do this IF statement 1-5
by jettero (Monsignor) on May 20, 2007 at 11:24 UTC
    The reason I like this solution the best (out of all the above, perhaps except /^[1-5]\z/) is that it's practical for values, even a large number of them, that aren't sequential.

    -Paul

Re^2: Quicker way to do this IF statement 1-5
by dewey (Pilgrim) on May 20, 2007 at 18:06 UTC
    This one gets my vote as well. Creating a hash to do the job is expressive, readable and general. Also helps you avoid hardcoding in the (1..5) values, letting you put them at the top where constants generally belong. demerphq++

    ~dewey