in reply to my 'if' condition doesn't work, why?

Yes, you can do it just fine with a regex, and see the other monks' answers. And TIMTOWTDI :-)

use Quantum::Superpositions 'any'; # OR #use Perl6::Junction 'any'; for (my $n = 0; $n < 268; $n++) { if ($n == any(40,47,76)) { print $n."\n"; } }

Replies are listed 'Best First'.
Re^2: my 'if' condition doesn't work, why?
by perltux (Monk) on Nov 11, 2014 at 22:00 UTC
    This looks like the 'nicest' (from a code readability and compactness point of view) solution to me, shame that it's not standard Perl but instead requires an extra module.

    So I have chosen to use
    if ($n==40 || $n==47 || $n==76) {
    which works fine too (regardless whether I use 'or' or '||').

    Many thanks to all who replied for your help.
      shame that it's not standard Perl but instead requires an extra module

      Well, you can get a similar result using List::Util, which is a core module:

      #! perl use strict; use warnings; use List::Util 'any'; for my $n (0 .. 267) { print "$n\n" if any { $n == $_ } (40, 47, 76); }

      Update: Fixed off-by-one error in the for loop range, thanks to AnomalousMonk.

      Output:

      17:34 >perl 1074_SoPW.pl 40 47 76 17:34 >

      Hope that helps,

      Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      "shame that it's not standard Perl but instead requires an extra module"

      Well, there does exist something similar in core Perl since 5.10: the smart match operator! Example:

      if ($n ~~ [40, 47, 76]) { ...; }

      However, smart match acts pretty weird in some cases (not the above case), so it is not usually a great idea to use it.