Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re^3: Smartmatch alternatives

by davido (Archbishop)
on Dec 17, 2013 at 16:05 UTC ( #1067488=note: print w/replies, xml ) Need Help??

in reply to Re^2: Smartmatch alternatives
in thread Smartmatch alternatives

No, I meant List::Util::any.

$ perl -MList::Util -E 'say "Yes" if List::Util::any { $_ == 42 } @{[q +w/1 3 42 5/]}' Yes

It shows up in both modules. There may be subtle, undocumented differences.


Replies are listed 'Best First'.
Re^4: Smartmatch alternatives
by LanX (Bishop) on Dec 17, 2013 at 16:13 UTC
    OK your CPAN- version doesn't show it in the synopsis

    SYNOPSIS use List::Util qw(first max maxstr min minstr reduce shuffle sum);

    and my 5.10 version does only list it as suggested.

    Anyway good news! (for me at least =)

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      :) Let's just say that whether one uses List::Util::any, or List::MoreUtils::any, it's a good idea to use "any" when "any" semantics are what is wanted.


Re^4: Smartmatch alternatives
by Anonymous Monk on Dec 17, 2013 at 16:14 UTC

    I see that it was added after 1.32 (most recent version installed here) ...

    1.33 -- Sun Oct 13 01:35 UTC 2013 * Added any, all, none, notall list reduction functions (inspired by List::MoreUtils)
      If you don't have any, it is easy to implement it using the reduce function of List::Utils with something like this:
      sub any { my $code_ref = shift; reduce { $a or $code_ref->(local $_ = $b) } @_; }
      Or, better:
      sub any(&@) { my $code_ref = shift; reduce { $a or $code_ref->(local $_ = $b) } @_; }
      which makes it possible to call it with a syntax similar to grep:
      <strike>print "true\n" if any { $_> 11 } qw /3 12 4 5 7/; # prints tru +e
      Update: the above line need to be this:
      print "true\n" if any { $_> 11 } 0, qw /3 12 4 5 7/; # prints true
        your any is always true!

        DB<12> use List::Util qw/reduce/ DB<13> sub any(&@) { my $code_ref = shift; reduce { $a or $code_ref->(local $_ = $b) } @_; } DB<15> print "true\n" if any { $_ < 0 } qw /3 12 4 5 7/; true

        Cheers Rolf

        ( addicted to the Perl Programming Language)

      Another good reason to run a recent Perl, I guess. ;) lol.

      Funny, I thought it went back farther than that.


        The most recent stable release of Perl only comes with List::Util 1.27.

        use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1067488]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (10)
As of 2017-12-13 18:44 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (373 votes). Check out past polls.