in reply to Sparing multiple 'or's

grep in scalar context

 grep { $a eq $_ }  'ABA', 'SCO', 'ACC', 'PHC' , 'GHF'

gives you the number of successful tests.

I.e. it's true in boolean context as long as it matches at least once.

HTH! :)

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^2: Sparing multiple 'or's
by BillKSmith (Monsignor) on Jun 04, 2018 at 16:15 UTC
    I would prefer to use the function 'any' from List::MoreUtils rather than 'grep' because the name seems to make the intention clearer. It may even be slightly faster because it can quit at the first match.
    Bill

      A bunch of stuff (everything?) from List::MoreUtils was migrated | copied to the core module List::Util some time ago, including any() used by Eily in the example here. (But List::MoreUtils is still around for those on ancient systems :)


      Give a man a fish:  <%-{-{-{-<

        How can I easily find out from which version on this is available in core?

        I checked 5.16 and 5.18 and both still say

        SUGGESTED ADDITIONS The following are additions that have been requested, but I hav +e been reluctant to add due to them being very simple to implement in +perl # One argument is true sub any { $_ && return 1 for @_; 0 }

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

      Sure , but

      >corelist List::MoreUtils Data for 2013-01-20 List::MoreUtils was not in CORE (or so I think)

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery