加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Perl 引用(即指针) 学习笔记

发布时间:2020-12-15 20:54:01 所属栏目:大数据 来源:网络整理
导读:前言 ????Perl引用就是指针,可以指向变量、数组、哈希表(也叫关联数组)甚至子程序。Pascal或C程序员应该对引用(即指针)的概念很熟悉,引用就是某值的地址,对其的使用则取决于程序员和语言的规定。在Perl中,可以把引用称为指针,二者是通用的,无差别

前言


????Perl引用就是指针,可以指向变量、数组、哈希表(也叫关联数组)甚至子程序。Pascal或C程序员应该对引用(即指针)的概念很熟悉,引用就是某值的地址,对其的使用则取决于程序员和语言的规定。在Perl中,可以把引用称为指针,二者是通用的,无差别的。引用在创建复杂数据方面十分有用。
??? 下面的内容为学习笔记,例子都测试过了,效果不错!

?

一、简单的例子

?

引用变量$pointer存的是$variable的地址,

而不是值本身,要获得值,形式为两个$符号

?

#!/usr/bin/perl -w
my $value = 10;
$pointer = /$value;
printf "/n Pointer Address $pointer of $value /n";
printf "/n What Pointer *($pointer) points to $$pointer/n";

打印结果:

Pointer Address SCALAR(0x182ba3c) of 10
What Pointer *(SCALAR(0x182ba3c)) points to 10

?

二、引用和数组

?

?????? $pointer 存储的是数组的地址,通过调用 scalar 函数去获得数组的个数

并且通过加 $ 解引用这个地址。

?

?

#!/usr/bin/perl -w
use strict;

my @array_demo = (1,2,3,4);
my $pointer = /@array_demo;
printf "Pointer Address of array_demo = $pointer/n";

#调用函数scalar()获得数组的元素个数
my $i = scalar(@$pointer);
printf "Number of arguments : $i /n";

#print the array all element
#通过引用访问哈希表的元素形式为$$pointer{$index},
$i = 0;
foreach (@$pointer) {
?printf "$i : $$pointer[$i++]; /n";
}

?

打印结果:

Pointer Address of array_demo = ARRAY(0x182ba84)
Number of arguments : 4
0 : 1;
1 : 2;
2 : 3;
3 : 4;

?

三、引用与哈希数列

?

?????? 与数组类似,通过引用访问哈希表的元素形式为$$pointer{$index},当然,$index是哈希表的键值,而不仅是数字。还有几种访问形式,此外,构建哈希表还可以用=>操作符,可读性更好些。下面再看一个例子:

?

#!/usr/bin/perl -w
use strict;

my %month = (
?"01" => 'Jan',
?"02" => 'Feb',
?"03" => 'Mar',
?"04" => 'Apr',
?"05" => 'May',
?"06" => 'Jun',
);

#print the hash address
my $pointer = /%month;
printf "/n Address of hash = $pointer/n ";

# The following lines would be used to print out the
# contents of the associative array if %month was used.
foreach my $i (sort keys %month) {
?printf "$i is $$pointer{$i} /n";
}

print "$$pointer{'02'}","/n";
print "$pointer->{'03'}","/n";

?

?

程序打印结果:

?

Address of hash = HASH(0x182ba1c)
01 is Jan
02 is Feb
03 is Mar
04 is Apr
05 is May
06 is Jun
Feb
Mar

思考一下,这里地址和实际的值使用上有什么区别?

?

print "$$pointer{'02'}","/n";??????? #这里Pointer 为地址
print "$month{'02'}","/n";?????????? #为实际的数值

print "$pointer->{'02'}","/n";????? #这个是比较方便的一种,速度块并且易读的一种写法

?

?

四、多维数组

?

#!/usr/bin/perl -w
use strict;

my $line = ['solid','black',['1','2','3',['4','5','6']]];
print "/$line->[0] = $line->[0] /n";
print "/$line->[1] = $line->[1] /n";
print "/$line->[2][0] = $line->[2][0] /n";
print "/$line->[2][1] = $line->[2][1] /n";
print "/$line->[2][2] = $line->[2][2] /n";
print "/$line->[2][3][0] = $line->[2][3][0] /n";
print "/$line->[2][3][1] = $line->[2][3][1] /n";
print "/$line->[2][3][2] = $line->[2][3][2] /n";

打印结果:

$line->[0] = solid
$line->[1] = black
$line->[2][0] = 1
$line->[2][1] = 2
$line->[2][2] = 3
$line->[2][3][0] = 4
$line->[2][3][1] = 5
$line->[2][3][2] = 6

?

?

五、引用与哈希

?

?

#!/usr/bin/perl -w
use strict;

my %cube = (
'0'=>['0','0','0'],
'1'=>['0','1'],
'2'=>['0','1',
'3'=>['0',
'4'=>['1',
'5'=>['1',
'6'=>['1',
'7'=>['1','1']
);
my $pointer = /%cube;

foreach my $i (sort keys %$pointer) {

?my $list = $$pointer{$i};
?my $z = $list->[0];
?my $x = $$pointer{$i}->[1];?????? #指向数组用 [] 指向哈希为{}
?my $y = $pointer->{$i}->[2];
?printf " Point $i = $x,$y,$z /n";
}

打印结果:

?Point 0 = 0,0
?Point 1 = 0,1,0
?Point 2 = 1,0
?Point 3 = 1,0
?Point 4 = 0,1
?Point 5 = 0,1
?Point 6 = 1,1
?Point 7 = 1,1

?


六、哈希与子函数

#!/usr/bin/perl
sub errorMsg {
? my $lvl = shift;
? #
? # define the subroutine to run when called.
? #
? return sub {
??? my $msg = shift; # Define the error type now.
??? print "Err Level $lvl:$msg/n"; }; # print later.
? }
$severe = errorMsg("Severe");
$fatal = errorMsg("Fatal");
$annoy = errorMsg("Annoying");

&$severe("Divide by zero");
&$fatal("Did you forget to use a semi-colon?");
&$annoy("Uninitialized variable in use");

七、 数组引用,

?

数组的地址 / 引用 为地址,

地址通过加入 @解引用

?

#!/usr/bin/perl -w
use strict;

my @names =(1,3);

print "$#names,@names";
my $address = /@names;
sub listem{
?? my ($list) = @_;
?? print (@$list[1],"/n");
}

&listem(/@names);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读