<1>数据引用与匿名存储
2016-11-28 14:11
225 查看
引用本身就是一种标量变量 引用变量,如 $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>数据引用与匿名存储
- 第<1>章 数据引用与匿名存储
- <NSCoding>存储数据 archivedDataWithRootObject unarchiveObjectWithData
- <Principles of fMRI 1>课程笔记8--fMRI的数据预处理
- <Android学习笔记6>——Android应用开发SharedPreferences存储数据的使用方法
- <Principles of fMRI 1>课程笔记1--fMRI简介以及数据分析
- <unity>关于快速的数据读取和存储——JSON(qwq)
- 第一章 数据引用与匿名存储
- <Principles of fMRI 1>课程笔记7--fMRI数据的时间分辨率和空间分辨率
- 第一章 数据引用与匿名存储
- <NSCoding>存储数据 archivedDataWithRootObject unarchiveObjectWithData
- <5 Android数据存储> 任务二 应用程序数据文件夹里的文件读写 ,
- “由于数据存储中不存在项目引用的设备平台,因此无法打开项目。”解决方法
- <转>反射 Type [DorpdowList绑定数据]
- <form>元素的几种引用方法
- android数据存储-->Files
- android数据存储-->SharedPerferences
- GridView中使用DataKeyNames存储数据键值<转载>
- Oracle重复数据的清除 <转>
- <转>高效的输入函数 --- 适用于ACM竞赛里输入数据特别多的情况