Smart match operator
Talk0
77pages on
this wiki
this wiki
In Perl6 the Smart Match Operator is ~~ and its inversed form is !~~.
Contents |
Description
Edit
The binary operator ~~ matchs a regular expression object to a string. The !~~ operator is !($str ~~ $regex).
Examples
Edit
Simple Demonstration
Edit
if 'part1' ~~ /<digit>/ {
say "Hello World";
}
| Output... |
|---|
Hello World |
Searching inside an array
Edit
my @list = 1,2,3; say (1 ~~ any(@list)) ?? "yes" !! "nope" ; say (6 ~~ any(@list)) ?? "yes" !! "nope" ;
| Output... |
|---|
yes nope |
Searching defined hash keys
Edit
my %pets = (cats=>1 ,dogs=>2, dodos=>0);
say ('pigs' ~~ any(keys %pets)) ?? "yes" !! "nope" ;
say ('cats' ~~ any(keys %pets)) ?? "yes" !! "nope" ;
| Output... |
|---|
nope yes |