<1>数据引用与匿名存储

引用本身就是一种标量变量

引用变量,如 $ra 或$rarray ,就是一种普通的标量变量,因为我们使用"$" 符号。


变量变量可以是一个整数,一个字符串或者一个引用,而且还可以被重新任意的赋值为这些数据类型中的任一种

Vsftp:/root/perl/10# cat a1.pl 
my $arr="111";

my $ref=$arr;

print $ref;
print "
";
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 "
";

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]
";

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]
";

print "@array is @array
";

my @arr2=@array[0,1,3];
print "@arr2 is @arr2
";

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 "
";
print @$rarray;
print "
";
print "11111111
";
print $$rarray[1];
print "
";
print "2222222222
";
print $rarray->[1];
print "
";

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 "
";

$rhash=\%hash;
print $$rhash{"dd"};
print "
";
print $rhash->{"dd"};
print "
";


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 "
";
print @$rarray;
print "
";


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
";
print "@array2 is @array2
";

sub AddArrays
{
   my ($rarray1,$rarray2) = @_;
    $len2 = @$rarray2; ##array2的长度
    print "$len2 is $len2
";
    for ($i = 0;$i<$len2;$i++){
      print "$i is $i
";
      my $str = $rarray1->[$i] + $rarray2->[$i];
      print "$str is $str
";
}
};
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
";
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 "
";
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 "
";
print $$rarray[1];
print "
";

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 "
";
print %sue;
print "
";

$sue{'child'}->[0]->{age}=99;
print $sue{'child'}->[0]->{age}; 
print "
";
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 "
";
Vsftp:/root/perl/10# perl a15.pl 
nameSuechildrenARRAY(0x1f01648)age45


引用的查询:

原文地址:https://www.cnblogs.com/zhaoyangjian724/p/6198955.html