Reputation: 517
I thought there would have been a simple answer for this somewhere on the internet but it seems like I'm having trouble finding a solution. I'm first of all wondering if there's a simple method or function for this:
e.g. ~~
or array.contains()
from Perl 5
It would also be nice to know how many different ways of achieving this result there are in Perl 6 as some might be better than others given the situation of the problem.
Upvotes: 15
Views: 1316
Reputation: 531
Sub first documentation. sub first
returns the matching element or Nil
. Nil
is a falsey value meaning you can use the result in a Bool
context to determine if the array contains matching element.
my @a = 'A'..'Z';
say 'We got it' if @a.first('Z');
say 'We didn\'t get it' if [email protected](1);
There are several adverbs to sub first
which change the results. For instance to return the index instead of the element it is possible use the :k
adverb. In this example we also topicalize the result for use within the if
statement:
my @a = 'A'..'Z';
if @a.first('Q', :k) -> $index {
say $index;
}
Upvotes: 5
Reputation:
my @a = <foo bar buzz>;
say so 'bar' ∈ @a;
say so @a ∋ 'buzz';
# OUTPUT«TrueTrue»
As documented in http://doc.perl6.org/language/setbagmix and defined in https://github.com/rakudo/rakudo/blob/nom/src/core/set_operators.pm .
I believe that Set
checks for equivalence, if you need identity you will have to loop over the array and ===
it.
You could turn the Array
into a Set
and use subscripts.
say @a.Set{'bar'};
# OUTPUT«True»
say @a.Set<bar buzz>;
# OUTPUT«(True True)»
Upvotes: 14
Reputation: 6840
Another way to do this, is:
my @a = <foo bar buzz>;
if 'bar' eq any(@a) {
say "yes";
}
# OUTPUT«yes»
Upvotes: 11