Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 655901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:39:21+00:00 2026-05-13T22:39:21+00:00

I’m looking for presence of an element in a list. In Python there is

  • 0

I’m looking for presence of an element in a list.

In Python there is an in keyword and I would do something like:

if element in list:
    doTask

Is there something equivalent in Perl without having to manually iterate through the entire list?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T22:39:21+00:00Added an answer on May 13, 2026 at 10:39 pm

    UPDATE:

    The smartmatch family of features are now experimental

    Smart match, added in v5.10.0 and significantly revised in v5.10.1, has been a regular point of complaint. Although there are a number of ways in which it is useful, it has also proven problematic and confusing for both users and implementors of Perl. There have been a number of proposals on how to best address the problem. It is clear that smartmatch is almost certainly either going to change or go away in the future. Relying on its current behavior is not recommended.

    Warnings will now be issued when the parser sees ~~, given, or when.




    If you can get away with requiring Perl v5.10, then you can use any of the following examples.

    • The smart match ~~ operator.

      if( $element ~~ @list ){ ... }
      if( $element ~~ [ 1, 2, 3 ] ){ ... }
      
    • You could also use the given/when construct. Which uses the smart match functionality internally.

      given( $element ){
         when( @list ){ ... }
      }
      
    • You can also use a for loop as a “topicalizer” ( meaning it sets $_ ).

      for( @elements ){
         when( @list ){ ... }
      }
      

    One thing that will come out in Perl 5.12 is the ability to use the post-fix version of when. Which makes it even more like if and unless.

    given( $element ){
      ... when @list;
    }
    

    If you have to be able to run on older versions of Perl, there still are several options.

    • You might think you can get away with using List::Util::first, but there are some edge conditions that make it problematic.

      In this example it is fairly obvious that we want to successfully match against 0. Unfortunately this code will print failure every time.

      use List::Util qw'first';
      my $element = 0;
      if( first { $element eq $_ } 0..9 ){
        print "success\n";
      } else {
        print "failure\n";
      }
      

      You could check the return value of first for defined-ness, but that will fail if we actually want a match against undef to succeed.

    • You can safely use grep however.

      if( grep { $element eq $_ } 0..9 ){ ... }
      

      This is safe because grep gets called in a scalar context. Arrays return the number of elements when called in scalar context. So this will continue to work even if we try to match against undef.

    • You could use an enclosing for loop. Just make sure you call last, to exit out of the loop on a successful match. Otherwise you might end up running your code more than once.

      for( @array ){
        if( $element eq $_ ){
          ...
          last;
        }
      }
      
    • You could put the for loop inside the condition of the if statement …

      if(
        do{
          my $match = 0;
          for( @list ){
            if( $element eq $_ ){
              $match = 1;
              last;
            }
          }
          $match; # the return value of the do block
        }
      ){
        ...
      }
      
    • … but it might be more clear to put the for loop before the if statement.

      my $match = 0;
      for( @list ){
        if( $_ eq $element ){
          $match = 1;
          last;
        }
      }
      
      if( $match ){ ... }
      
    • If you’re only matching against strings, you could also use a hash. This can speed up your program if @list is large and, you are going to match against %hash several times. Especially if @array doesn’t change, because then you only have to load up %hash once.

      my %hash = map { $_, 1 } @array;
      if( $hash{ $element } ){ ... }
      
    • You could also make your own subroutine. This is one of the cases where it is useful to use prototypes.

      sub in(&@){
        local $_;
        my $code = shift;
        for( @_ ){ # sets $_
          if( $code->() ){
            return 1;
          }
        }
        return 0;
      }
      
      if( in { $element eq $_ } @list ){ ... }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.