develooper Front page | perl.perl6.language | Postings from July 2010

Re: Array membership test?

Thread Previous | Thread Next
From:
Jonathan Worthington
Date:
July 29, 2010 16:52
Subject:
Re: Array membership test?
Message ID:
4C521432.7090105@jnthn.net
Mark J. Reed wrote:
> Possibly a FAQ, but is there a simple way of asking if an item is
> contained in an array?  I know of $x ~~ any(@array) and @array.grep({
> $_ ~~ $x}), but those both seem a bit complicated for a conceptually
> simple test, so I'm wondering if I'm missing something.
Note that grep doesn't have to take a closure, but can also take just 
the value you're looking for...

 > my @x = 1,2,3; say ?@x.grep(2); say ?@x.grep(4);
1
0

Though more efficient would be:

 > my @x = 1,2,3; say ?@x.first(2); say ?@x.first(4);
1
0

I still prefer the junction way though. :-)

/jnthn


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About