问题
Here's what I'm trying to do. It should be very simple, but I can't figure out how to do it correctly.
> my @search_keys = <bb cc dd>
[bb cc dd]
> my $search_junc = @search_keys.join('|')
bb|cc|dd
> "bb" eq $search_junc
False
回答1:
my @search_keys = <bb cc dd>;
say "bb" eq any(@search_keys); # any(True, False, False)
say so "bb" eq any(@search_keys); # True
The |
syntax is merely sugar for calling the any()
function. Just like &
is syntactic sugar for the all()
function. They both return Junction
s, which you can collapse with e.g. the so
function. Of course, if you're going to use it in a conditional, you don't need to collapse it yourself, the Bool
ification of the condition will do that for you:
say "found" if "bb" eq any(@search_keys);
See also: https://docs.raku.org/type/Junction
EDIT (more than 2 years later):
If you are interested in the simple equivalence of the given object ("bb"
) in the list (<bb cc dd>
), you can also use set operators for that:
say "found" if "bb" (elem) @search_keys; # found
Technically, this will do the comparison on the .WHICH
of the given strings. More importantly, this idiom will short-cut as soon as a match is found. So since in your example "bb"
is the first element in the array, it will only check that element. And it won't need to build any additional objects, like a Junction
(in the first solution) or a Set
(in the second solution).
来源:https://stackoverflow.com/questions/46927870/making-a-junction-out-of-an-array-with-string-values-in-perl-6