On Tue, Jan 25, 2011 at 12:08 PM, Ed Avis <eda@waniasset.com> wrote: > This question is about the smart match operator ~~ and about its documentation. > Is it possible to find whether an array contains a given string? > > In perl 5.10.0 I was using > > @a ~~ $x > > but from a glance at perlsyn(1) it appears that this does something different in > 5.10.1 and later. I am not posting to complain about that change - it was well > signposted that smart matching in 5.10.0 was an experimental feature - but I > suggest an explicit entry in the 'Smart matching in detail' section to document > the case when $a is an array and $b is a string, which is currently missing. > > From the new perlsyn it looks like 'match against an array element' might be > what I want to use, as > > $x ~~ @a > > but this has some surprising behaviour: > > % perl -E '@a = (1); say "1 x" ~~ @a' > 1 > > That is a consequence of this: > > % perl -E 'say "1 x" ~~ 1' > 1 > > which is documented in the 'Any / Num' case in perlsyn (and gives a warning > with -w). But is there any way to get a plain string match, at least for > scalars which are not references? > > If not, I could instead use > > List::MoreUtils::any { $_ eq $x } @a Actually, all you would have to do is make sure that the elements of the array are strings. perl -E '@a = ("1"); say "1 x" ~~ @a'Thread Previous | Thread Next