in reply to Length of multidimensional array based on the particular index value

grep in scalar context... evil? It works.

say scalar grep{ $_->[2] } @select;

There's also 'for'

my $length; $length++ if $_->[2] for @select; say $length;

Dave

Replies are listed 'Best First'.
Re^2: Length of multidimensional array based on the particular index value
by Eliya (Vicar) on Feb 24, 2011 at 10:23 UTC
    $length++ if $_->[2] for @select;

    Unfortunately, Perl doesn't understand this "double modifier" syntax (syntax error at..., near "] for "), so it would have to be:

    do { $length++ if $_->[2] } for @select;
      it would have to be: do { $length++ if $_->[2] } for @select;

      Can also be:

      $_->[2] and $length++ for @select;

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        And since $_->[2] is either 0 or 1 you could do:

        $length+=$_->[2] for @select;

      there you go taking all my fun away!

      Here's an awful contortion:

      $_->[2]?length++:0 for @select;

      Dave

        $_->[2]?length++:0 for @select;

        Why are you trying to increment the length function?

        or even

        $length += !!$_->[2] for @select;

        :)

        (the !! is only required if you don't want to make assumptions that a "true" value in $_->[2] is always represented as 1)