perl--匿名数据
2015-09-21 17:03
344 查看
在C里,间接的最常见的形式就是指针,它可以让一个变量保存另外一个变量的内存地址。
在Perl里,间接的最常见的形式是引用。
什么是引用?
在我们的例子里,$vitals[0] 的值是”john”.
也就是说它正好包含另外一个(全局)变量的名字,我们说第一个变量提到了第二个变量,
并且这种参考叫符号引用,因为Perl必须在一个符号表找出@john 来才到能找到它。
8.2 创建引用
创建引用的方法有好多种,我们在讲述它们的时候大多数先描述它们,然后才解释如何使用(解引用)所生成的引用。
8.2.1 反斜杠操作符
你可以用一个反斜杠创建一个指向任何命名变量或者子过程的引用。(
你还可以把它用于一个匿名标量值,比如7或”camel”,尽管你通常并不需要这些东西)乍一看,这个操作符的作用类似
C里的&(取址)操作符
[root@master Webqq]# cat t12.pl
$scalarred = \$foo;
$constref = \186_282.42;
$arrayref = \@ARGV;
$hashref = \%ENV;
$coderef = \&handler;
print “$scalarred is $scalarred\n”;
print “$constref is $constref\n”;
print “$arrayref is $arrayref\n”;
[root@master Webqq]# perl t12.pl
scalarredisSCALAR(0x10b4470)scalarred is SCALAR(0x10b4470)
constref is SCALAR(0x1097e38)
$arrayref is ARRAY(0x10b4068
8.2.2.1 匿名数组组合器
你可以用方括号创建一个指向匿名属组的引用:
$arrayref = [1,2,[‘a’,’b’,’c’,’d’]];
[root@master Webqq]# cat t13.pl
arrayref=[1,2,[‘a′,′b′,′c′,′d′]];print“arrayref = [1,2,[‘a’,’b’,’c’,’d’]];
print “arrayref->[2][2]\n”;
[root@master Webqq]# perl t13.pl
c
2维数组,第2列数组的第3个元素
现在我们有一个方法来表示我们本章开头的表:
[root@master Webqq]# cat t13.pl
table=[[“join”,47,”brown”,186],[“mary”,23,”hazel”,128],[“bill”,35,”blue”,157]];printtable =[[“join”,47,”brown”,186],
[“mary”,23,”hazel”,128],
[“bill”,35,”blue”,157]];
print table->[0][1];
print table−>[1][1];printtable->[1][1];
print table->[2][1];
[root@master Webqq]# perl t13.pl
472335[root@master Webqq]#
8.2.2.2 匿名散列组合器
你可以用花括号创建一个指向匿名散列的引用:
[root@master Webqq]# cat t14.pl
hashref=‘Adam′=>‘Eve′,‘Clyde′=>$bonnie,‘Antony′=>‘Cleo′.′patra′;printhashref ={
‘Adam’ => ‘Eve’,
‘Clyde’ => $bonnie,
‘Antony’ => ‘Cleo’.’patra’
};
print hashref;
print “\n”;
print $$hashref{Antony};
[root@master Webqq]# perl t14.pl
HASH(0x20f6d48)
Cleopatra[root@master Webqq]#
[root@master Webqq]#
[root@master Webqq]# cat t15.pl
table=“john”=>[47,“brown”,186],“mary”=>[23,“hazel”,128],“bill”=>[35,“blue”,157],;printtable = {
“john” => [47, “brown”, 186],
“mary” => [23, “hazel”, 128],
“bill” => [35, “blue”, 157],
};
print table;
print “\n”;
print tablejohn[1];printtable{john}[1];
print table{john}[2];
[root@master Webqq]# perl t15.pl
HASH(0x18c9710)
brown186[root@master Webqq]#
这是一个数组散列,选择最好的数据结构是难度很高的工种,下一章专门讲这个。
下面介绍散列的散列
[root@master Webqq]# cat t16.pl
table=“john”=>age=>47,eyes=>“brown”,weight=>186,,“mary”=>age=>23,eyes=>“hazel”,weight=>128,,“bill”=>age=>35,eyes=>“blue”,weight=>157,,;printtable = {
“john” => { age => 47,
eyes => “brown”,
weight => 186,
},
“mary” => { age => 23,
eyes => “hazel”,
weight => 128,
},
“bill” => { age => 35,
eyes => “blue”,
weight => 157,
},
};
print table;
print “\n”;
print $$table{john}{age};
[root@master Webqq]# perl t16.pl
HASH(0xe696a8)
47[root@master Webqq]#
8.2.2.3 匿名子过程组合器
你可以通过不带子过程名字的sub创建一个匿名子过程
[root@master Webqq]# cat t17.pl
coderef = sub { print "Boink!\n" }; # 现在 &coderef = sub { print "Boink!\n" }; # 现在 &coderef 打印 “Boink!”
print &$coderef;
[root@master Webqq]# perl t17.pl
Boink!
1[root@master Webqq]#
&$$coderef; 子程序解引用
在Perl里,间接的最常见的形式是引用。
什么是引用?
在我们的例子里,$vitals[0] 的值是”john”.
也就是说它正好包含另外一个(全局)变量的名字,我们说第一个变量提到了第二个变量,
并且这种参考叫符号引用,因为Perl必须在一个符号表找出@john 来才到能找到它。
8.2 创建引用
创建引用的方法有好多种,我们在讲述它们的时候大多数先描述它们,然后才解释如何使用(解引用)所生成的引用。
8.2.1 反斜杠操作符
你可以用一个反斜杠创建一个指向任何命名变量或者子过程的引用。(
你还可以把它用于一个匿名标量值,比如7或”camel”,尽管你通常并不需要这些东西)乍一看,这个操作符的作用类似
C里的&(取址)操作符
[root@master Webqq]# cat t12.pl
$scalarred = \$foo;
$constref = \186_282.42;
$arrayref = \@ARGV;
$hashref = \%ENV;
$coderef = \&handler;
print “$scalarred is $scalarred\n”;
print “$constref is $constref\n”;
print “$arrayref is $arrayref\n”;
[root@master Webqq]# perl t12.pl
scalarredisSCALAR(0x10b4470)scalarred is SCALAR(0x10b4470)
constref is SCALAR(0x1097e38)
$arrayref is ARRAY(0x10b4068
8.2.2.1 匿名数组组合器
你可以用方括号创建一个指向匿名属组的引用:
$arrayref = [1,2,[‘a’,’b’,’c’,’d’]];
[root@master Webqq]# cat t13.pl
arrayref=[1,2,[‘a′,′b′,′c′,′d′]];print“arrayref = [1,2,[‘a’,’b’,’c’,’d’]];
print “arrayref->[2][2]\n”;
[root@master Webqq]# perl t13.pl
c
2维数组,第2列数组的第3个元素
现在我们有一个方法来表示我们本章开头的表:
[root@master Webqq]# cat t13.pl
table=[[“join”,47,”brown”,186],[“mary”,23,”hazel”,128],[“bill”,35,”blue”,157]];printtable =[[“join”,47,”brown”,186],
[“mary”,23,”hazel”,128],
[“bill”,35,”blue”,157]];
print table->[0][1];
print table−>[1][1];printtable->[1][1];
print table->[2][1];
[root@master Webqq]# perl t13.pl
472335[root@master Webqq]#
8.2.2.2 匿名散列组合器
你可以用花括号创建一个指向匿名散列的引用:
[root@master Webqq]# cat t14.pl
hashref=‘Adam′=>‘Eve′,‘Clyde′=>$bonnie,‘Antony′=>‘Cleo′.′patra′;printhashref ={
‘Adam’ => ‘Eve’,
‘Clyde’ => $bonnie,
‘Antony’ => ‘Cleo’.’patra’
};
print hashref;
print “\n”;
print $$hashref{Antony};
[root@master Webqq]# perl t14.pl
HASH(0x20f6d48)
Cleopatra[root@master Webqq]#
[root@master Webqq]#
[root@master Webqq]# cat t15.pl
table=“john”=>[47,“brown”,186],“mary”=>[23,“hazel”,128],“bill”=>[35,“blue”,157],;printtable = {
“john” => [47, “brown”, 186],
“mary” => [23, “hazel”, 128],
“bill” => [35, “blue”, 157],
};
print table;
print “\n”;
print tablejohn[1];printtable{john}[1];
print table{john}[2];
[root@master Webqq]# perl t15.pl
HASH(0x18c9710)
brown186[root@master Webqq]#
这是一个数组散列,选择最好的数据结构是难度很高的工种,下一章专门讲这个。
下面介绍散列的散列
[root@master Webqq]# cat t16.pl
table=“john”=>age=>47,eyes=>“brown”,weight=>186,,“mary”=>age=>23,eyes=>“hazel”,weight=>128,,“bill”=>age=>35,eyes=>“blue”,weight=>157,,;printtable = {
“john” => { age => 47,
eyes => “brown”,
weight => 186,
},
“mary” => { age => 23,
eyes => “hazel”,
weight => 128,
},
“bill” => { age => 35,
eyes => “blue”,
weight => 157,
},
};
print table;
print “\n”;
print $$table{john}{age};
[root@master Webqq]# perl t16.pl
HASH(0xe696a8)
47[root@master Webqq]#
8.2.2.3 匿名子过程组合器
你可以通过不带子过程名字的sub创建一个匿名子过程
[root@master Webqq]# cat t17.pl
coderef = sub { print "Boink!\n" }; # 现在 &coderef = sub { print "Boink!\n" }; # 现在 &coderef 打印 “Boink!”
print &$coderef;
[root@master Webqq]# perl t17.pl
Boink!
1[root@master Webqq]#
&$$coderef; 子程序解引用
相关文章推荐
- iOS开发-处理字符串中的双引号
- android NotificationCompat.Builder 使用
- No matching authentication protocol错误
- SQL Server数据库优化的10多种方法
- arpspoof dnsspoof中间人攻击
- 对话框显示图像的简单方法
- ios9升级后导致的多语言问题
- 15.Android 异步更新UI 技巧
- Spring的AOP中before,afterReturning,afterThrowing参数说明
- block_dump观察Linux IO写入的具体文件
- Singleton
- xib自定义cell,代码修改cell里控件frame加载后不生效,下一次reloaddata改变?
- memcache CAS实现原子操作
- 数据库SQL优化大总结之 百万级数据库优化方案
- Selenium的自我总结2_元素基本操作
- 第三周项目2-建设“顺序表”算法库
- 详细解析JavaFX的键盘事件KeyEvent - Pressd/Released/Typed
- sdk接入
- »Spring 之AOP AspectJ切入点语法详解(最全了,不需要再去其他地找了)
- boost的字符串处理函数——string algorithm