<1>数据引用与匿名存储
- 引用本身就是一种标量变量
- 引用变量,如 $ra 或$rarray ,就是一种普通的标量变量,因为我们使用"$" 符号。
- 变量变量可以是一个整数,一个字符串或者一个引用,而且还可以被重新任意的赋值为这些数据类型中的任一种
- Vsftp:/root/perl/10# cat a1.pl
- my $arr="111";
- my $ref=\$arr;
- print $ref;
- print "\n";
- Vsftp:/root/perl/10# perl a1.pl
- SCALAR(0xa50e38)
- 间接访问:
- 间接访问的意思就是取得引用所指的变量的值
- Vsftp:/root/perl/10# cat a2.pl
- use Data::Dumper;
- my $var="aa";
- my $key=\$var;
- $hash{$key}=11;
- print Dumper(%hash);
- print "\n";
- Vsftp:/root/perl/10# perl a2.pl
- $VAR1 = 'SCALAR(0x1f19438)';
- $VAR2 = 11;
- 对标量变量的引用
- 对数组的引用:
- Vsftp:/root/perl/10# cat a5.pl
- my @array=qw/ ab bc cd de ef/;
- print "\$array[2] is $array[2]\n";
- Vsftp:/root/perl/10# perl a5.pl
- $array[2] is cd
- Vsftp:/root/perl/10# cat a5.pl
- my @array=qw/ ab bc cd de ef/;
- print "\$array[2] is $array[2]\n";
- print "\@array is @array\n";
- my @arr2=@array[0,1,3];
- print "\@arr2 is @arr2\n";
- Vsftp:/root/perl/10# perl a5.pl
- $array[2] is cd
- @array is ab bc cd de ef
- @arr2 is ab bc de
- 简明的箭头记号:
- Perl 提供了另一种简单易读的用以存取数组和散列表元素的语法结构:"->[]" 记号
- Vsftp:/root/perl/10# cat a6.pl
- my @array=qw/ab bc cd de ef/;
- $rarray=\@array;
- print $rarray;
- print "\n";
- print @$rarray;
- print "\n";
- print "11111111\n";
- print $$rarray[1];
- print "\n";
- print "2222222222\n";
- print $rarray->[1];
- print "\n";
- Vsftp:/root/perl/10# perl a6.pl
- ARRAY(0xf82450)
- abbccddeef
- 11111111
- bc
- 2222222222
- bc
- 与数组类似, 你可以通过使用->{} 记号来存取散列表中的数据元素:
- Vsftp:/root/perl/10# cat a7.pl
- %hash=("a"=>"b","c1"=>"d2","dd"=>"eq21");
- print %hash;
- print "\n";
- $rhash=\%hash;
- print $$rhash{"dd"};
- print "\n";
- print $rhash->{"dd"};
- print "\n";
- Vsftp:/root/perl/10# perl a7.pl
- abddeq21c1d2
- eq21
- eq21
- 不存在自动间接访问:
- Vsftp:/root/perl/10# cat a8.pl
- $rarray=\@array;
- push ($rarray ,1,2,3);
- Vsftp:/root/perl/10# perl a8.pl
- Type of arg 1 to push must be array (not scalar dereference) at a8.pl line 2, near "3)"
- Execution of a8.pl aborted due to compilation errors.
- push 第一个参数必须是数组
- Vsftp:/root/perl/10# cat a8.pl
- $rarray=\@array;
- push (@$rarray ,1,2,3);
- print $rarray;
- print "\n";
- print @$rarray;
- print "\n";
- Vsftp:/root/perl/10# perl a8.pl
- ARRAY(0x211f480)
- 123
- 使用引用:
- Vsftp:/root/perl/10# cat a9.pl
- @array1=(1,2,3);
- @array2=(4,5,6,7);
- AddArrays (\@array1,\@array2);## 以引用方式传递数组
- print "\@array1 is @array1\n";
- print "\@array2 is @array2\n";
- sub AddArrays
- {
- my ($rarray1,$rarray2) = @_;
- $len2 = @$rarray2; ##array2的长度
- print "\$len2 is $len2\n";
- for ($i = 0;$i<$len2;$i++){
- print "\$i is $i\n";
- my $str = $rarray1->[$i] + $rarray2->[$i];
- print "\$str is $str\n";
- }
- };
- Vsftp:/root/perl/10# perl a9.pl
- $len2 is 4
- $i is 0
- $str is 5
- $i is 1
- $str is 7
- $i is 2
- $str is 9
- $i is 3
- $str is 7
- @array1 is 1 2 3
- @array2 is 4 5 6 7
- 运行效率:
- 通过使用引用,我们可以高效的向子例程中传入或传出大量数据
- 匿名存储的引用:
- 到目前为止,我们学习创建了对已存在变量的引用,现在我们要学习对"匿名"数据结构的引用,
- 也就是那些没有同变量名关联的值
- 创建匿名数组,需要使用方括号而不是圆括号,如:
- $ra = []; ##创建一个空的匿名数组,并返回对它的引用
- $ra = [1,"hello"];## 创建一个经过
- Vsftp:/root/perl/10# cat a10.pl
- my $rh=(1,"hello");
- print "\$rh is $rh\n";
- Vsftp:/root/perl/10# perl a10.pl
- $rh is hello
- ## 这是一个普通的使用%前缀的散列表,它由圆括号所包围的列表初始化
- Vsftp:/root/perl/10# cat a11.pl
- %hash = ("flock"=>"birds","pride" =>"lions");
- print %hash;
- print "\n";
- Vsftp:/root/perl/10# perl a11.pl
- flockbirdspridelions
- ##匿名散列表是一组用大括号括起来的列表
- Vsftp:/root/perl/10# perl a12.pl
- $a is
- $$a is hello world
- 多重引用的间接访问:
- Vsftp:/root/perl/10# cat a13.pl
- $rarray=[a1,b2,c3,d4,e5];
- print $rarray->[1];
- print "\n";
- print $$rarray[1];
- print "\n";
- Vsftp:/root/perl/10# perl a13.pl
- b2
- b2
- 嵌套数据结构:
- 20Vsftp:/root/perl/10# cat a14.pl
- %sue = (
- 'name' => 'Sue',
- 'age' => '45');
- %john = (
- 'name' =>'John',
- 'age' =>'20');
- %peggy = (
- 'name' => 'Peggy',
- 'age' =>'16');
- @children = (\%john,\%peggy);
- $sue{'child'} = \@children;
- print $sue{'child'}->[0]->{age};
- Vsftp:/root/perl/10# perl a14.pl
- 20Vsftp:/root/perl/10#
- 隐含的创建复杂的数据结构:
- Vsftp:/root/perl/10# cat a14.pl
- %sue = (
- 'name' => 'Sue',
- 'age' => '45');
- %john = (
- 'name' =>'John',
- 'age' =>'20');
- %peggy = (
- 'name' => 'Peggy',
- 'age' =>'16');
- @children = (\%john,\%peggy);
- $sue{'child'} = \@children;
- print $sue{'child'}->[0]->{age};
- print "\n";
- print %sue;
- print "\n";
- $sue{'child'}->[0]->{age}=99;
- print $sue{'child'}->[0]->{age};
- print "\n";
- Vsftp:/root/perl/10# perl a14.pl
- 20
- nameSuechildARRAY(0x1e356a8)age45
- 99
- 最后的缩写:省去两个下表间的箭头:
- Vsftp:/root/perl/10# cat a15.pl
- %sue = (
- 'name' => 'Sue',
- 'age' =>'45',
- 'children' =>[
- {
- 'name' =>' John',
- 'age' =>'20'
- },
- {'name' =>'Peggy',
- 'age' =>'16'
- }
- ]
- );
- print %sue;
- print "\n";
- Vsftp:/root/perl/10# perl a15.pl
- nameSuechildrenARRAY(0x1f01648)age45
- 引用的查询:
<1>数据引用与匿名存储的更多相关文章
- 数据引用Data References
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- [SAP ABAP开发技术总结]数据引用(data references)、对象引用(object references)
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- java数据的5种存储位置(转)
任何语言所编写的程序,其中的各类型的数据都需要一个存储位置,java中书的存储位置分为以下5种: 1.寄存器 最快的存储区,位于处理器内部,但是数量及其有限.所以寄存器根据需求自动分配,无序人为控制. ...
- 亿级用户下的新浪微博平台架构 前端机(提供 API 接口服务),队列机(处理上行业务逻辑,主要是数据写入),存储(mc、mysql、mcq、redis 、HBase等)
https://mp.weixin.qq.com/s/f319mm6QsetwxntvSXpKxg 亿级用户下的新浪微博平台架构 炼数成金前沿推荐 2014-12-04 序言 新浪微博在2014年3月 ...
- Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (一) —— 总览
Android数据的四种存储方式SharedPreferences.SQLite.Content Provider和File (一) —— 总览 作为一个完成的应用程序,数据存储操作是必不可少的. ...
- Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (三) —— SharePreferences
除了SQLite数据库外,SharedPreferences也是一种轻型的数据存储方式,它的本质是基于XML文件存储key-value键值对数据,通常用来存储一些简单的配置信息.其存储位置在/data ...
- Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (四) —— ContentProvider
ContentProvider是安卓平台中,在不同应用程序之间实现数据共享的一种机制.一个应用程序如果需要让别的程序可以操作自己的数据,即可采用这种机制.并且此种方式忽略了底层的数据存储实现,Cont ...
- Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (二) —— SQLite
SQLite是一种转为嵌入式设备设计的轻型数据库,其只有五种数据类型,分别是: NULL: 空值 INTEGER: 整数 REAL: 浮点数 TEXT: 字符串 BLOB: 大数据 在SQLite中, ...
- 使用vlookup嵌套INDIRECT函数实现跨表数据引用
这是一个使用 vlookup 函数嵌套 INDIRECT 函数来实现跨工作表数据引用的教程. 某小学决定要抽查本校三年级三个班的期末考情况,抽查方法为在每个班中抽查5名学生,将这15名学生的期末考情况 ...
随机推荐
- C#读取Excel文档
上面分别是Excel文档的内容和读取结果: 奉上C#源代码: using System; using System.Data; using System.Data.OleDb; namespace R ...
- php导出execl
<?php function export_excel($items,$fields,$fields_array,$name) { /* * 调用方法示例 * $items = $this-&g ...
- oracle定时备份
1.将如下代码复制到文本中,最后将文本后缀名称修改成XXX.bat 批处理文件: *********************************************************** ...
- fastjson的坑 com.alibaba.fastjson.JSONObject cannot be cast to xxx
解析json对象时,使用了new TypeReference()对象 fastjson会对解析的对象类型进行缓存 new TypeReference<ResultData>(){} ...
- 切换到mint了,纪念一下
- PhoneGap与WAP站静态化
最近在参与的WAP站项目,决定将所有页面都静态化处理,登录验证.价格数据等都通ajax动态的方式实现.开始这么规划的目前是为了页面提高网站加载速度及SEO,最近看到了一篇报道phonegap buil ...
- WORDPRESS开发(一)自定义页面显示分类目录
第一步:自定义一个页面,如index.php 第二步:打开index.php文件,引用wp-blog-header.php文件 require('wp-blog-header.php'); 第三步使用 ...
- jquery ajax post, get, javascript ajax post, get 处理
ajax 创建 XMLHttp 对象IE7 以上的版本都支持 XMLHttpRequestIE7 以下的用 ActiveXObject async:true, // 当false 时,当执行完这个才 ...
- 使用reinterpret_cast的危险
关键字: c++ cast // Cast.cpp : Defines the entry point for the console application. // #include "s ...
- MySQL下Limit使用及性能分析
对于一直用Oracle的我,今天可是非常诧异,MySQL中同一个函数在不同数量级上的性能居然差距如此之大. 先看表ibmng(id,title,info) 唯一 id key 索引title 先看 ...