in reply to Re^3: Minimisation of codes (if grep {})
in thread Minimisation of codes (if grep {})

#!/usr/bin/perl use Modern::Perl; use List::Util qw(first max maxstr min minstr reduce shuffle sum); my @pantry = ("Panc", "Pancake", "Panmix"); my $total = 0; $total++ if( any { Panc } @pantry); #$total++ if grep { /^\Qpanc\E$/ } @pantry; print "$total";

Here its showing error. Cant use Barewords Panc at 9

Replies are listed 'Best First'.
Re^5: Minimisation of codes (if grep {})
by choroba (Cardinal) on Oct 01, 2015 at 11:28 UTC
    Instead of
    any { Panc }

    use

    any { /^\QPanc\E$/ }

    the same way you would do with grep.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      #!/usr/bin/perl use Modern::Perl; use List::Util qw(first max maxstr min minstr reduce shuffle sum); my @pantry = ("Panc", "Pancake", "Panmix"); my $total = 0; $total++ if any { /^\QPanc\E$/ } @pantry; #$total++ if grep { /^\Qpanc\E$/ } @pantry; print "$total";

      Errors: Use of uninitialized value $_ in pattern match (m//) at /root/prac/CDR/tr.pl line 8. Can't locate object method "any" via package "Panc" (perhaps you forgot to load "Panc"?) at /root/prac/CDR/tr.pl line 8.

        You're trying to find an element in a list. But, I can't find the element "any" in your function list on line 3. The function wasn't imported.
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ