๐Ÿ“˜ Check if an element is in a list in Perl 6

๐Ÿ“˜ Check if an element is in a list in Raku

N. B. Perl 6 has been renamed to Raku. Click to read more.


Tell if the given value is in the list.

There are a few approaches to the problem. The most compact one seems to be the use of theย smartmatch ~~ operator in combination with theย anyfunction:

my @array = (10, 14, 0, 15, 17, 20, 30, 35);
my $x = 17;
say 'In the list' if $x ~~ any @array;

To check if a given value is contained among the elements of an array or a list, use the greproutine.

say 'In the list' if grep $x, @array;

Theย grep routine returns a list of all the matched elements. In the Boolean context, the return value isย True if at least one element was found. In the opposite case, an empty list coerces toย False. If the element in question is not zero, then theย first routine may be used instead of grep. It returns the first element that matches the search pattern:

say 'In the list' if first $x, @array;

To extend this to zero values, test the values before making a decision:

say 'In the list' if $x == first $x, @array;

Another solution is to convert the array to a hash and check if there is a key with the given value. This is useful when you need more than one check.

my %hash = map {$_ => 1}, @array;
say 'In the list' if %hash{$n};

4 thoughts on “๐Ÿ“˜ Check if an element is in a list in Perl 6”

  1. Andrew,

    Thanks for the examples. The “~~” operator made for some interesting reading after I saw the “any” option as I wondered what the other options were. I found a detailed write-up here: (https://docs.raku.org/language/operators#infix_~~). What a neat language.

    One thing that still bothered me is that Python has a more terse option than the ones above as you can do something like:
    “a” in [“a”, “b”, “c”]
    and get a True or False returned iirc. A general principle that I’ve seen in Raku is that not only is there more than one way to do it, but that there is a much terser method as well than 99.9% of the existing tools out there. After a little digging, I came up upon this alternative, which although less general than some of the methods you have in this section, is terse and readable to those used to that notation:
    1 โˆ‰
    The above returns with True if 1 is a member of the array. In my eyes, this is slightly more terse than Python and even more readable once you’re familiar with the “โˆ‰” symbol.

    It is pretty neat that Raku seems to take some of the better ideas from the APL family of languages where a function to average numbers of a list, matrix, or list of matrices would be something like:
    Average โ† {(+/ฯ‰)รท(ฯฯ‰)}
    But manages to integrate that power in a modern language without the downsides of APL which is less powerful outside the arena of numbers. I’m starting to see the elegance of Raku.

    1. It looks like my raku statement got cut off. Let me try again:

      “1 โˆ‰ ” will return True, but disregard the quotes.

  2. Alright. Last comment. It seems like this blog has trouble with some of the special characters and is chopping off the array I’ve tried to include.

    I made a mistake by using the wrong operator. I meant to use “โˆˆ”. What seems strange though is that “โˆ‰” gave me the same answer as “โˆˆ” for testing inclusion when I used an array using brackets instead of parenthesis (). Not sure what is going on there. I thought both forms of creating an array were identical as in versus (1,2,3). I guess I have more reading to do, or it is a bug (probably just user error).

    1. Figured out what characters were causing the issue w/ the blog at least. When entering an array using the less-than (Shift + ,) and greater than (Shift + .) it gets thrown out entirely, so it makes the previous few posts non-nonsensical. Sorry a/b that.

      I’m still confused why I get a different answer when using one form of literal list (example: parenthesis or square brackets) versus the less-than & greater-than notation as I thought. using less-than & greater-than notation weirdness goes deeper. When separating the elements with spaces or “,” commas, only the separation with spaces allows me to grab a subelement such as @array[1].

      I used Rakudo v2021.07, Raku v6.d, & MoarVM 2021.07 for my above tests.

      I apologize for all of the posts. Feel free to delete all :).

Leave a Reply

Your email address will not be published. Required fields are marked *

Retype the CAPTCHA code from the image
Change the CAPTCHA codeSpeak the CAPTCHA code