perl智能匹配
1.perl中~~为智能匹配,它能够智能地依据符号两側的操作数来确定操作。
如要推断某个元素是否存在于数组中,不使用智能匹配,程序像这样:
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";
}
使用智能匹配,程序像这样:
my $x=2;
my @array;
if(@array~~$x)
{
print "$x is in the array";
}
else
{
print "$x is in the array";
}
再如查看哈希表中是否存在某个键值
‘hello’~~{hello=>1,world=>2} 结果为true
2.智能匹配的优先级在perl的在线文档中,智能匹配会依照表格的顺序选择匹配,优先级从上到下逐渐减少。
$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
perl智能匹配的更多相关文章
- Perl语言学习笔记 15 智能匹配与give-when结构
1.智能匹配操作符 替代绑定操作符: 在哈希中查找某一个键: 比較两个数组是否全然同样: 查找列表中是否存在某个元素: 智能匹配操作符与顺序无关.~~ 左右元素能够互换 2.智能操作符优先级 3.gi ...
- perl 跨行匹配;
<pre name="code" class="html"><pre name="code" class="ht ...
- android音乐播放器开发 SweetMusicPlayer 智能匹配本地歌词
上一篇写了使用MediaPlayer播放音乐,http://blog.csdn.net/huweigoodboy/article/details/39861539. 代码地址:https://gith ...
- fastjson简单使用demo,@JSONField注解属性字段上与set、get方法上。实体类toString(),实体类转json的区别;_下划线-减号大小写智能匹配
一.demo代码 @JSONField注解属性字段上与set.get方法上.使用@Data注解(lombok插件安装最下方),对属性“笔名”[pseudonym]手动重写setter/getter方法 ...
- Perl 正则匹配经验记录
0.正则匹配字典:http://www.cnblogs.com/itech/archive/2010/03/19/1689793.html 1.$/=">";重新定义字符串分 ...
- perl 如何匹配ASCII码以及ASCII码转换
匹配ASCII码: /[:ascii:]/ ASCII码转换为数字: ord() 数字转换为ASCII码: chr()
- perl 正则匹配多个
Vsftp:/root# cat k1.pl my $_='upDaTe'; if( $_ =~ /^(SELECT|UPDATE|DELETE|INSERT|SET|COMMIT|ROLLBACK| ...
- perl 贪婪匹配小例子
redis01:/root# cat x2.pl my $str="a19823a456123"; if ($str =~/a(.*)23/){print "1----& ...
- 学生&部门智能匹配程序
Github链接 结对成员: 031502308 付逸豪 031502113 胡俊钦 数据生成 样例链接: 来点我啊 由于在生成数据的时候做了很多符合实际情况的限制,所以生成的数据都挺好的,就随便选了 ...
随机推荐
- Java 实现模板方法(TemplateMethod)模式
类图 /** * 业务流程模板.提供基本框架 * @author stone * */ public abstract class BaseTemplate { public abstract voi ...
- java web中 classpath路径 详解
在使用ssh等框架开发web程序时配置文件(xml和properties)存放的路径一般为src下,当部署程序时则必须存在于classes路径下,具体如下 src不是classpath, WEB-IN ...
- htmlspecialchars_decode
htmlspecialchars_decode htmlspecialchars_decode - 将特殊的 HTML 实体转换回普通字符 htmlspecialchars - 将特殊字符转换为 ...
- JavaScript 匹配浮点数
//匹配整数,正小数 appUtils.bindEvent($(_pageId+" .remind-list-con input"),function(){ var reg = / ...
- jquery插件:aotocomplete
aotocomplete.js http://blog.csdn.net/smeyou/article/details/7980273?_t_t_t=0.3565731019350138 $(func ...
- 打造Android万能上拉下拉刷新框架--XRefreshView(三)
转载请注明出处:http://blog.csdn.net/footballclub/ 打造Android万能上拉下拉刷新框架–XRefreshView(一) 打造Android万能上拉下拉刷新框架–X ...
- Jenkins与Docker相关的Plugin使用
原文地址:http://blog.csdn.net/ztsinghua/article/details/52128140 Jenkins与Docker相关的Plugin 在Jenkins Plugin ...
- 工作总结 错误 using 块缺少结束字符“}”。请确保此块内的所有“{”都有匹配的“}”字符,并且任何“}”都不会解释为标记。
页面上 有两个 它会跟标签 匹配的 标准要在同一级别下 什么也不改变 只改变它们位置 就不报错了 总结 @using (Html.BeginForm()) { } 要根据标签位置 匹配 要放 ...
- PATHINFO模式是thinkphp特有的吗?
pathinfo当然不是某个框架特有的,pathinfo严格上讲是HTTP服务器提供的一个预定义变量,在许多的框架中有一个重要的组件叫做路由器,这个组件可以通过使用pathinfo来实现. 考虑以下代 ...
- PAT 1001. A+B Format(水题)
#include<cstdio> #include<cstring> using namespace std; char s[10]; int main() { int a,b ...