选择显示字体大小

perl教学 第十二篇 perl5中的引用之二

运行结果如下:

$ test 1 2 3 4
pointer address of argv = array(0x806c378)
number of arguments : 4
0 : 1;
1 : 2;
2 : 3;
3 : 4;
第5行将引用$pointer指向数组@argv,第6行输出argv的地址。$pointer返回数组第一个元素的地址,这与c语言中的数组指针是类似的。第7行调用函数scalar()获得数组的元素个数,该参数亦可为@argv,但用指针则必须用@$pointer的形式指定其类型为数组,$pointer给出地址,@符号说明传递的地址为数组的第一个元素的地址。第10行与第7行类似,第11行用形式$$pointer[$i]列出所有元素。
对关联数组使用反斜线操作符的方法是一样的--把所有关联数组名换成引用$poniter。注意数组和简单变量(标量)的引用显示时均带有类型--array和scalar,哈希表(关联数组)和函数也一样,分别为hash和code。下面是哈希表的引用的例子。

#!/usr/bin/perl
1 #
2 # using associative array references
3 #
4 %month = (
5 '01', 'jan',
6 '02', 'feb',
7 '03', 'mar',
8 '04', 'apr',
9 '05', 'may',
10 '06', 'jun',
11 '07', 'jul',
12 '08', 'aug',
13 '09', 'sep',
14 '10', 'oct',
15 '11', 'nov',
16 '12', 'dec',
17 );
18
19 $pointer = \%month;
20
21 printf "\n address of hash = $pointer\n ";
22
23 #
24 # the following lines would be used to print out the
25 # contents of the associative array if %month was used.
26 #
27 # foreach $i (sort keys %month) {
28 # printf "\n $i $$pointer{$i} ";
29 # }
30
31 #
32 # the reference to the associative array via $pointer
33 #
34 foreach $i (sort keys %$pointer) {
35 printf "$i is $$pointer{$i} \n";
36 }
结果输出如下:

$ mth
address of hash = hash(0x806c52c)
01 is jan
02 is feb
03 is mar
04 is apr
05 is may
06 is jun
07 is jul
08 is aug
09 is sep
10 is oct
11 is nov
12 is dec


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons