1. 引用本身就是一种标量变量
  2.  
  3. 引用变量,如 $ra $rarray ,就是一种普通的标量变量,因为我们使用"$" 符号。
  4.  
  5. 变量变量可以是一个整数,一个字符串或者一个引用,而且还可以被重新任意的赋值为这些数据类型中的任一种
  6.  
  7. Vsftp:/root/perl/10# cat a1.pl
  8. my $arr="111";
  9.  
  10. my $ref=\$arr;
  11.  
  12. print $ref;
  13. print "\n";
  14. Vsftp:/root/perl/10# perl a1.pl
  15. SCALAR(0xa50e38)
  16.  
  17. 间接访问:
  18.  
  19. 间接访问的意思就是取得引用所指的变量的值
  20.  
  21. Vsftp:/root/perl/10# cat a2.pl
  22. use Data::Dumper;
  23. my $var="aa";
  24. my $key=\$var;
  25. $hash{$key}=11;
  26. print Dumper(%hash);
  27. print "\n";
  28.  
  29. Vsftp:/root/perl/10# perl a2.pl
  30. $VAR1 = 'SCALAR(0x1f19438)';
  31. $VAR2 = 11;
  32.  
  33. 对标量变量的引用
  34.  
  35. 对数组的引用:
  36.  
  37. Vsftp:/root/perl/10# cat a5.pl
  38. my @array=qw/ ab bc cd de ef/;
  39. print "\$array[2] is $array[2]\n";
  40.  
  41. Vsftp:/root/perl/10# perl a5.pl
  42. $array[2] is cd
  43.  
  44. Vsftp:/root/perl/10# cat a5.pl
  45. my @array=qw/ ab bc cd de ef/;
  46. print "\$array[2] is $array[2]\n";
  47.  
  48. print "\@array is @array\n";
  49.  
  50. my @arr2=@array[0,1,3];
  51. print "\@arr2 is @arr2\n";
  52.  
  53. Vsftp:/root/perl/10# perl a5.pl
  54. $array[2] is cd
  55. @array is ab bc cd de ef
  56. @arr2 is ab bc de
  57.  
  58. 简明的箭头记号:
  59.  
  60. Perl 提供了另一种简单易读的用以存取数组和散列表元素的语法结构:"->[]" 记号
  61.  
  62. Vsftp:/root/perl/10# cat a6.pl
  63. my @array=qw/ab bc cd de ef/;
  64. $rarray=\@array;
  65. print $rarray;
  66. print "\n";
  67. print @$rarray;
  68. print "\n";
  69. print "11111111\n";
  70. print $$rarray[1];
  71. print "\n";
  72. print "2222222222\n";
  73. print $rarray->[1];
  74. print "\n";
  75.  
  76. Vsftp:/root/perl/10# perl a6.pl
  77. ARRAY(0xf82450)
  78. abbccddeef
  79. 11111111
  80. bc
  81. 2222222222
  82. bc
  83.  
  84. 与数组类似, 你可以通过使用->{} 记号来存取散列表中的数据元素:
  85.  
  86. Vsftp:/root/perl/10# cat a7.pl
  87. %hash=("a"=>"b","c1"=>"d2","dd"=>"eq21");
  88. print %hash;
  89. print "\n";
  90.  
  91. $rhash=\%hash;
  92. print $$rhash{"dd"};
  93. print "\n";
  94. print $rhash->{"dd"};
  95. print "\n";
  96.  
  97. Vsftp:/root/perl/10# perl a7.pl
  98. abddeq21c1d2
  99. eq21
  100. eq21
  101.  
  102. 不存在自动间接访问:
  103.  
  104. Vsftp:/root/perl/10# cat a8.pl
  105. $rarray=\@array;
  106. push ($rarray ,1,2,3);
  107.  
  108. Vsftp:/root/perl/10# perl a8.pl
  109. Type of arg 1 to push must be array (not scalar dereference) at a8.pl line 2, near "3)"
  110. Execution of a8.pl aborted due to compilation errors.
  111.  
  112. push 第一个参数必须是数组
  113.  
  114. Vsftp:/root/perl/10# cat a8.pl
  115. $rarray=\@array;
  116. push (@$rarray ,1,2,3);
  117. print $rarray;
  118. print "\n";
  119. print @$rarray;
  120. print "\n";
  121.  
  122. Vsftp:/root/perl/10# perl a8.pl
  123. ARRAY(0x211f480)
  124. 123
  125.  
  126. 使用引用:
  127.  
  128. Vsftp:/root/perl/10# cat a9.pl
  129. @array1=(1,2,3);
  130. @array2=(4,5,6,7);
  131. AddArrays (\@array1,\@array2);## 以引用方式传递数组
  132.  
  133. print "\@array1 is @array1\n";
  134. print "\@array2 is @array2\n";
  135.  
  136. sub AddArrays
  137. {
  138. my ($rarray1,$rarray2) = @_;
  139. $len2 = @$rarray2; ##array2的长度
  140. print "\$len2 is $len2\n";
  141. for ($i = 0;$i<$len2;$i++){
  142. print "\$i is $i\n";
  143. my $str = $rarray1->[$i] + $rarray2->[$i];
  144. print "\$str is $str\n";
  145. }
  146. };
  147. Vsftp:/root/perl/10# perl a9.pl
  148. $len2 is 4
  149. $i is 0
  150. $str is 5
  151. $i is 1
  152. $str is 7
  153. $i is 2
  154. $str is 9
  155. $i is 3
  156. $str is 7
  157. @array1 is 1 2 3
  158. @array2 is 4 5 6 7
  159.  
  160. 运行效率:
  161.  
  162. 通过使用引用,我们可以高效的向子例程中传入或传出大量数据
  163.  
  164. 匿名存储的引用:
  165.  
  166. 到目前为止,我们学习创建了对已存在变量的引用,现在我们要学习对"匿名"数据结构的引用,
  167.  
  168. 也就是那些没有同变量名关联的值
  169.  
  170. 创建匿名数组,需要使用方括号而不是圆括号,如:
  171.  
  172. $ra = []; ##创建一个空的匿名数组,并返回对它的引用
  173.  
  174. $ra = [1,"hello"];## 创建一个经过
  175.  
  176. Vsftp:/root/perl/10# cat a10.pl
  177. my $rh=(1,"hello");
  178. print "\$rh is $rh\n";
  179. Vsftp:/root/perl/10# perl a10.pl
  180. $rh is hello
  181.  
  182. ## 这是一个普通的使用%前缀的散列表,它由圆括号所包围的列表初始化
  183.  
  184. Vsftp:/root/perl/10# cat a11.pl
  185. %hash = ("flock"=>"birds","pride" =>"lions");
  186. print %hash;
  187. print "\n";
  188. Vsftp:/root/perl/10# perl a11.pl
  189. flockbirdspridelions
  190.  
  191. ##匿名散列表是一组用大括号括起来的列表
  192.  
  193. Vsftp:/root/perl/10# perl a12.pl
  194. $a is
  195. $$a is hello world
  196.  
  197. 多重引用的间接访问:
  198.  
  199. Vsftp:/root/perl/10# cat a13.pl
  200. $rarray=[a1,b2,c3,d4,e5];
  201. print $rarray->[1];
  202. print "\n";
  203. print $$rarray[1];
  204. print "\n";
  205.  
  206. Vsftp:/root/perl/10# perl a13.pl
  207. b2
  208. b2
  209.  
  210. 嵌套数据结构:
  211.  
  212. 20Vsftp:/root/perl/10# cat a14.pl
  213. %sue = (
  214. 'name' => 'Sue',
  215. 'age' => '45');
  216.  
  217. %john = (
  218. 'name' =>'John',
  219. 'age' =>'20');
  220.  
  221. %peggy = (
  222. 'name' => 'Peggy',
  223. 'age' =>'16');
  224.  
  225. @children = (\%john,\%peggy);
  226. $sue{'child'} = \@children;
  227.  
  228. print $sue{'child'}->[0]->{age};
  229. Vsftp:/root/perl/10# perl a14.pl
  230. 20Vsftp:/root/perl/10#
  231.  
  232. 隐含的创建复杂的数据结构:
  233.  
  234. Vsftp:/root/perl/10# cat a14.pl
  235. %sue = (
  236. 'name' => 'Sue',
  237. 'age' => '45');
  238.  
  239. %john = (
  240. 'name' =>'John',
  241. 'age' =>'20');
  242.  
  243. %peggy = (
  244. 'name' => 'Peggy',
  245. 'age' =>'16');
  246.  
  247. @children = (\%john,\%peggy);
  248. $sue{'child'} = \@children;
  249.  
  250. print $sue{'child'}->[0]->{age};
  251. print "\n";
  252. print %sue;
  253. print "\n";
  254.  
  255. $sue{'child'}->[0]->{age}=99;
  256. print $sue{'child'}->[0]->{age};
  257. print "\n";
  258. Vsftp:/root/perl/10# perl a14.pl
  259. 20
  260. nameSuechildARRAY(0x1e356a8)age45
  261. 99
  262.  
  263. 最后的缩写:省去两个下表间的箭头:
  264.  
  265. Vsftp:/root/perl/10# cat a15.pl
  266. %sue = (
  267. 'name' => 'Sue',
  268. 'age' =>'45',
  269. 'children' =>[
  270. {
  271. 'name' =>' John',
  272. 'age' =>'20'
  273. },
  274. {'name' =>'Peggy',
  275. 'age' =>'16'
  276. }
  277. ]
  278. );
  279. print %sue;
  280. print "\n";
  281. Vsftp:/root/perl/10# perl a15.pl
  282. nameSuechildrenARRAY(0x1f01648)age45
  283.  
  284. 引用的查询:

<1>数据引用与匿名存储的更多相关文章

  1. 数据引用Data References

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  2. [SAP ABAP开发技术总结]数据引用(data references)、对象引用(object references)

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  3. java数据的5种存储位置(转)

    任何语言所编写的程序,其中的各类型的数据都需要一个存储位置,java中书的存储位置分为以下5种: 1.寄存器 最快的存储区,位于处理器内部,但是数量及其有限.所以寄存器根据需求自动分配,无序人为控制. ...

  4. 亿级用户下的新浪微博平台架构 前端机(提供 API 接口服务),队列机(处理上行业务逻辑,主要是数据写入),存储(mc、mysql、mcq、redis 、HBase等)

    https://mp.weixin.qq.com/s/f319mm6QsetwxntvSXpKxg 亿级用户下的新浪微博平台架构 炼数成金前沿推荐 2014-12-04 序言 新浪微博在2014年3月 ...

  5. Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (一) —— 总览

    Android数据的四种存储方式SharedPreferences.SQLite.Content Provider和File (一) —— 总览   作为一个完成的应用程序,数据存储操作是必不可少的. ...

  6. Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (三) —— SharePreferences

    除了SQLite数据库外,SharedPreferences也是一种轻型的数据存储方式,它的本质是基于XML文件存储key-value键值对数据,通常用来存储一些简单的配置信息.其存储位置在/data ...

  7. Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (四) —— ContentProvider

    ContentProvider是安卓平台中,在不同应用程序之间实现数据共享的一种机制.一个应用程序如果需要让别的程序可以操作自己的数据,即可采用这种机制.并且此种方式忽略了底层的数据存储实现,Cont ...

  8. Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (二) —— SQLite

    SQLite是一种转为嵌入式设备设计的轻型数据库,其只有五种数据类型,分别是: NULL: 空值 INTEGER: 整数 REAL: 浮点数 TEXT: 字符串 BLOB: 大数据 在SQLite中, ...

  9. 使用vlookup嵌套INDIRECT函数实现跨表数据引用

    这是一个使用 vlookup 函数嵌套 INDIRECT 函数来实现跨工作表数据引用的教程. 某小学决定要抽查本校三年级三个班的期末考情况,抽查方法为在每个班中抽查5名学生,将这15名学生的期末考情况 ...

随机推荐

  1. C#读取Excel文档

    上面分别是Excel文档的内容和读取结果: 奉上C#源代码: using System; using System.Data; using System.Data.OleDb; namespace R ...

  2. php导出execl

    <?php function export_excel($items,$fields,$fields_array,$name) { /* * 调用方法示例 * $items = $this-&g ...

  3. oracle定时备份

    1.将如下代码复制到文本中,最后将文本后缀名称修改成XXX.bat 批处理文件: *********************************************************** ...

  4. fastjson的坑 com.alibaba.fastjson.JSONObject cannot be cast to xxx

    解析json对象时,使用了new TypeReference()对象 fastjson会对解析的对象类型进行缓存   new TypeReference<ResultData>(){}   ...

  5. 切换到mint了,纪念一下

  6. PhoneGap与WAP站静态化

    最近在参与的WAP站项目,决定将所有页面都静态化处理,登录验证.价格数据等都通ajax动态的方式实现.开始这么规划的目前是为了页面提高网站加载速度及SEO,最近看到了一篇报道phonegap buil ...

  7. WORDPRESS开发(一)自定义页面显示分类目录

    第一步:自定义一个页面,如index.php 第二步:打开index.php文件,引用wp-blog-header.php文件 require('wp-blog-header.php'); 第三步使用 ...

  8. jquery ajax post, get, javascript ajax post, get 处理

    ajax 创建 XMLHttp 对象IE7 以上的版本都支持 XMLHttpRequestIE7 以下的用 ActiveXObject async:true,  // 当false 时,当执行完这个才 ...

  9. 使用reinterpret_cast的危险

    关键字: c++ cast // Cast.cpp : Defines the entry point for the console application. // #include "s ...

  10. MySQL下Limit使用及性能分析

    对于一直用Oracle的我,今天可是非常诧异,MySQL中同一个函数在不同数量级上的性能居然差距如此之大. 先看表ibmng(id,title,info)  唯一  id key 索引title 先看 ...