Ωmega
Ωmega

Reputation: 43673

The smartmatch operator is not working as expected

Why the smartmatch operator ~~ says that 0 is not in (0, 5..100)?

print ((0 ~~ (0, 5..100)) ? "Y" : "N");

N

Test it here.

Upvotes: 3

Views: 126

Answers (2)

ikegami
ikegami

Reputation: 385897

Don't use the broken smart-match operator. 0 ~~ ... is specifically one of the reasons it's considered broken.

Use

grep { $_ } 0, 5..100

or

use List::Util qw( first );

first { $_ } 0, 5..100

Upvotes: 2

mob
mob

Reputation: 118605

Make the right hand side an array reference

print ((0 ~~ [0, 5..100]) ? "Y" : "N");

or a named array

@a = (0, 5..100);
print ((0 ~~ @a) ? "Y" : "N");

or a ... whatever this is called (anonymous named array?)

print ((0 ~~ @{[0,5..100]}) ? "Y" : "N");

(0,5..100) is a list but it is not an array, and this is one of the places where the distinction is important.

Upvotes: 5

Related Questions