Hi,
I was writting a long list of OR's (||) in several IF statements, then it occured to me. I'm sure there is way of listing all the or values rather than repeating the same variable and each individual value. After searching on the net, I haven't come up with anything, so I've come for some sage wisdom from the best place possible.
IF ($variable == 1 || $variable == 2 || $variable == 3) {
...
IF ($vaiables == [1,2,3?????????
Lyle
Update: Great to see so many responses and expecially great to see that Perl6 will have an a'any' function to deal with this nicely. In my opinion for overall speed and efficiency bageler's solution is best:-
sub any {
my $var = shift;
for (@_) { return 1 if $var == $_ }
}
if (any($var,1..2000)) { print "Var is between 1 and 2000 inclusive\n"
+ }
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.