Perl intelligent match

Source: Internet
Author: User

1. perl ~~ For intelligent matching, it can intelligently Determine operations based on the operands on both sides of the symbol.

To determine whether an element exists in an array, do not use smart matching. The program is like this:

my $x=2;my @array=(1,2,3);my $flag=0;for (@array){if($x==$_){$flag=1;}}if($flag == 1){print "$x is in the array";}else{print "$x is not in the array";}

Use smart matching, the program is like this:

my $x=2;my @array;if(@array~~$x){print "$x is in the array";}else{print "$x is in the array";}

Check whether a key value exists in the hash table.

'Hello '~~ {Hello => 1, world => 2} returns true

2. The priority of intelligent matching is in the online perl document. Intelligent Matching selects matching based on the order of the table, and the priority gradually decreases from top to bottom.

     $a      $b        Type of Match Implied    Matching Cod    ======  =====     =====================    =============    Hash    Hash      hash keys identical      [sort keys %$a]~~[sort keys %$b]    Hash    Array     hash slice existence     grep {exists $a->{$_}} @$b    Hash    Regex     hash key grep            grep /$b/, keys %$a    Hash    Any       hash entry existence     exists $a->{$b}    Array   Array     arrays are identical[*]    Array   Regex     array grep               grep /$b/, @$a    Array   Num       array contains number    grep $_ == $b, @$a    Array   Any       array contains string    grep $_ eq $b, @$a    Any     undef     undefined                !defined $a    Any     Regex     pattern match            $a =~ /$b/    Code()  Code()    results are equal        $a->() eq $b->()    Any     Code()    simple closure truth     $b->() # ignoring $a    Num     numish[!] numeric equality         $a == $b    Any     Str       string equality          $a eq $b    Any     Num       numeric equality         $a == $b    Any     Any       string equality          $a eq $b

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.