Search code examples
arrayslistperlsmartmatch

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.


Solution

  • 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.