Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Perl arrays and references

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

这篇文章将为大家详细讲解有关Perl数组和引用怎么用,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

一、Perl数组和引用

关于Perl语言应该记住的最重要的一点可能是:Perl中的Perl数组和哈希表始终是一维的。因此,Perl数组和哈希表只保存标量值,不直接存贮Perl数组或其它的复杂数据结构。Perl数组的成员要么是数(或字符串)要么是引用。

对Perl数组和哈希表可以象对简单变量一样使用反斜线操作符,Perl数组的引用如下:

1#!/usr/bin/perl 2# 3#UsingArrayreferences 4# 5$pointer=\@ARGV; 6printf"\nPointerAddressofARGV=$pointer\n"; 7$i=scalar(@$pointer); 8printf"\nNumberofarguments:$i\n"; 9$i=0; 10foreach(@$pointer){ 11printf"$i:$$pointer[$i++];\n"; 12}

运行结果如下:

$test1234 PointerAddressofARGV=ARRAY(0x806c378) Numberofarguments:4 0:1; 1:2; 2:3;

3:4;第5行将引用$pointer指向Perl数组@ARGV,第6行输出ARGV的地址。$pointer返回Perl数组***个元素的地址,这与C语言中的Perl数组指针是类似的。第7行调用函数scalar()获得Perl数组的元素个数,该参数亦可为@ARGV,但用指针则必须用@$pointer的形式指定其类型为Perl数组,$pointer给出地址,@符号说明传递的地址为Perl数组的***个元素的地址。第10行与第7行类似,第11行用形式$$pointer[$i]列出所有元素。

对关联Perl数组使用反斜线操作符的方法是一样的--把所有关联Perl数组名换成引用$poniter。注意Perl数组和简单变量(标量)的引用显示时均带有类型--ARRAY和SCALAR,哈希表(关联Perl数组)和函数也一样,分别为HASH和CODE。

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

1#!/usr/bin/perl 2# 3#UsingArrayreferences 4# 5%weekday=( 6'01'=>'Mon', 7'02'=>'Tue', 8'03'=>'Wed', 9'04'=>'Thu', 10'05'=>'Fri', 11'06'=>'Sat', 12'07'=>'Sun', 13); 14$pointer=\%weekday; 15$i='05'; 16printf"\n==================starttest=================\n"; 17# 18#Thesenexttwolinesshouldshowanoutput 19# 20printf'$$pointer{$i}is'; 21printf"$$pointer{$i}\n"; 22printf'${$pointer}{$i}is'; 23printf"${$pointer}{$i}\n"; 24printf'$pointer->{$i}is'; 25 26printf"$pointer->{$i}\n"; 27# 28#Thesenexttwolinesshouldnotshowanything29# 30printf'${$pointer{$i}}is'; 31printf"${$pointer{$i}}\n"; 32printf'${$pointer->{$i}}is'; 33printf"${$pointer->{$i}}"; 34printf"\n==================endoftest=================\n"; 35

结果输出如下:

==================starttest================= $$pointer{$i}isFri ${$pointer}{$i}isFri $pointer->{$i}isFri ${$pointer{$i}}is ${$pointer->{$i}}is ==================endoftest=================可以看到,前三种形式的输出显示了预期的结果,而后两种则没有。当你不清楚是否正确时,就输出结果看看。在Perl中,有不明确的代码就用print语句输出来实验一下,这能使你清楚Perl是怎样解释你的代码的。

二、多维Perl数组

语句@array=list;可以创建Perl数组的引用,中括号可以创建匿名Perl数组的引用。下面语句为用于画图的三维Perl数组的例子:

$line=['solid','black',['1','2','3'],['4','5','6']];

此语句建立了一个含四个元素的三维Perl数组,变量$line指向该Perl数组。前两个元素是标量,存贮线条的类型和颜色,后两个元素是匿名Perl数组的引用,存贮线条的起点和终点。访问其元素语法如下:

$arrayReference->[$index]single-dimensionalarray $arrayReference->[$index1][$index2]two-dimensionalarray $arrayReference->[$index1][$index2][$index3]three-dimensionalarray

可以创建在你的智力、设计经验和计算机的内存允许的情况下极尽复杂的结构,但***对可能读到或管理你的代码的人友好一些--尽量使代码简单些。另一方面,如果你想向别人炫耀你的编程能力,Perl给你足够的机会和能力编写连自己都难免糊涂的代码。:)

建议:当你想使用多于三维的Perl数组时,***考虑使用其它数据结构来简化代码。

下面为创建和使用二维Perl数组的例子:

1#!/usr/bin/perl 2# 3#UsingMulti-dimensionalArrayreferences 4# 5$line=['solid','black',['1','2','3'],['4','5','6']]; 6print"\$line->[0]=$line->[0]\n"; 7print"\$line->[1]=$line->[1]\n"; 8print"\$line->[2][0]=$line->[2][0]\n"; 9print"\$line->[2][1]=$line->[2][1]\n"; 10print"\$line->[2][2]=$line->[2][2]\n"; 11print"\$line->[3][0]=$line->[3][0]\n"; 12print"\$line->[3][1]=$line->[3][1]\n"; 13print"\$line->[3][2]=$line->[3][2]\n"; 14print"\n";#Theobligatoryoutputbeautifier

.

结果输出如下:

$line->[0]=solid $line->[1]=black $line->[2][0]=1 $line->[2][1]=2 $line->[2][2]=3 $line->[3][0]=4 $line->[3][1]=5 $line->[3][2]=6关于"Perl数组和引用怎么用"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report