小白教程
所有教程
关于
Search
172.69.7.158
172.69.7.158
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Perl 数组”的源代码
本页内容
上一节:
Perl_标量
下一节:
Perl_哈希
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:Perl 数组}}[[Category:Perl 教程|8]] = Perl 数组 = Perl 数组一个是存储标量值的列表变量,变量可以是不同类型。 数组变量以 @ 开头。访问数组元素使用 '''$ + 变量名称 + [索引值]''' 格式来读取,实例如下: <sample title="" desc="" lang="perl" hererun="1"> #!/usr/bin/perl @hits = (25, 30, 40); @names = ("google", "xiaobai", "taobao"); print "\$hits[0] = $hits[0]\n"; print "\$hits[1] = $hits[1]\n"; print "\$hits[2] = $hits[2]\n"; print "\$names[0] = $names[0]\n"; print "\$names[1] = $names[1]\n"; print "\$names[2] = $names[2]\n"; </sample> 程序中 $ 符号使用了 \ 来转义,让他原样输出。 执行以上程序,输出结果为: <pre> $hits[0] = 25 $hits[1] = 30 $hits[2] = 40 $names[0] = google $names[1] = xiaobai $names[2] = taobao </pre> == 创建数组 == 数组变量以 '''@''' 符号开始,元素放在括号内,也可以以 '''qw''' 开始定义数组。 <pre> @array = (1, 2, 'Hello'); @array = qw/这是 一个 数组/; </pre> 第二个数组使用 '''qw//''' 运算符,它返回字符串列表,数组元素以空格分隔。当然也可以使用多行来定义数组: <pre> @days = qw/google taobao ... xiaobai/; </pre> 你也可以按索引来给数组赋值,如下所示: <pre> $array[0] = 'Monday'; ... $array[6] = 'Sunday'; </pre> == 访问数组元素 == 访问数组元素使用 '''$ + 变量名称 + [索引值]''' 格式来读取,实例如下: <sample title="" desc="" lang="perl" hererun="1"> @sites = qw/google taobao xiaobai/; print "$sites[0]\n"; print "$sites[1]\n"; print "$sites[2]\n"; print "$sites[-1]\n"; # 负数,反向读取 </sample> 执行以上程序,输出结果为: <pre> google taobao xiaobai xiaobai </pre> 数组索引值从 0 开始,即 0 为第一个元素,1 为第二个元素,以此类推。 负数从反向开始读取,-1 为第一个元素, -2 为第二个元素 == 数组序列号 == Perl 提供了可以按序列输出的数组形式,格式为 '''起始值 + .. + 结束值''',实例如下: <sample title="" desc="" lang="perl" hererun="1"> #!/usr/bin/perl @var_10 = (1..10); @var_20 = (10..20); @var_abc = ('a'..'z'); print "@var_10\n"; # 输出 1 到 10 print "@var_20\n"; # 输出 10 到 20 print "@var_abc\n"; # 输出 a 到 z </sample> 执行以上程序,输出结果为: <pre> 1 2 3 4 5 6 7 8 9 10 10 11 12 13 14 15 16 17 18 19 20 a b c d e f g h i j k l m n o p q r s t u v w x y z </pre> == 数组大小 == 数组大小由数组中的标量上下文决定: <sample title="" desc="" lang="perl" hererun="1"> @array = (1,2,3); print "数组大小: ",scalar @array,"\n"; </sample> 执行以上程序,输出结果为: <pre>数组大小: 3</pre> 数组长度返回的是数组物理大小,而不是元素的个数,我们可以看以下实例: <sample title="" desc="" lang="perl" hererun="1"> #!/uer/bin/perl @array = (1,2,3); $array[50] = 4; $size = @array; $max_index = $#array; print "数组大小: $size\n"; print "最大索引: $max_index\n"; </sample> 执行以上程序,输出结果为: <pre>数组大小: 51 最大索引: 50</pre> 从输出的结果可以看出,数组元素只有四个,但是数组大小为 51。 == 添加和删除数组元素 == Perl 提供了一些有用的函数来添加和删除数组元素。 如果你之前没有编程经验,可能会问什么是函数,其实我们之前使用的 '''print''' 即是一个输出函数。 下表列出了数组中常用的操作函数: {| class="table table-striped table-hover" !width="50%"| 序号 !width="50%"| 类型和描述 |- | 1 | '''push @ARRAY, LIST''' 将列表的值放到数组的末尾 |- | 2 | '''pop @ARRAY''' 删除数组的最后一个值 |- | 3 | '''shift @ARRAY''' 弹出数组第一个值,并返回它。数组的索引值也依次减一。 |- | 4 | '''unshift @ARRAY, LIST''' 将列表放在数组前面,并返回新数组的元素个数。 |} <sample title="" desc="" lang="perl" hererun="1"> #!/usr/bin/perl # 创建一个简单是数组 @sites = ("google","xiaobai","taobao"); $new_size = @sites ; print "1. \@sites = @sites\n"."原数组长度 :$new_size\n"; # 在数组结尾添加一个元素 $new_size = push(@sites, "baidu"); print "2. \@sites = @sites\n"."新数组长度 :$new_size\n"; # 在数组开头添加一个元素 $new_size = unshift(@sites, "weibo"); print "3. \@sites = @sites\n"."新数组长度 :$new_size\n"; # 删除数组末尾的元素 $new_byte = pop(@sites); print "4. \@sites = @sites\n"."弹出元素为 :$new_byte\n"; # 移除数组开头的元素 $new_byte = shift(@sites); print "5. \@sites = @sites\n"."弹出元素为 :$new_byte\n"; </sample> 执行以上程序,输出结果为: == 切割数组 == 我们可以切割一个数组,并返回切割后的新数组: <sample title="" desc="" lang="perl" hererun="1"> #!/usr/bin/perl @sites = qw/google taobao xiaobai weibo qq facebook 网易/; @sites2 = @sites[3,4,5]; print "@sites2\n"; </sample> 执行以上程序,输出结果为: <pre> weibo qq facebook </pre> 数组索引需要指定有效的索引值,可以是正数后负数,每个索引值使用逗号隔开。 如果是连续的索引,可以使用 '''..''' 来表示指定范围: <sample title="" desc="" lang="perl" hererun="1"> #!/usr/bin/perl @sites = qw/google taobao xiaobai weibo qq facebook 网易/; @sites2 = @sites[3..5]; print "@sites2\n"; </sample> 执行以上程序,输出结果为: <pre> weibo qq facebook </pre> == 替换数组元素 == Perl 中数组元素替换使用 splice() 函数,语法格式如下: <pre> splice @ARRAY, OFFSET [ , LENGTH [ , LIST ] ] </pre> 参数说明: * @ARRAY:要替换的数组。 * OFFSET:起始位置。 * LENGTH:替换的元素个数。 * LIST:替换元素列表。 以下实例从第6个元素开始替换数组中的5个元素: <sample title="" desc="" lang="perl" hererun="1"> #!/usr/bin/perl @nums = (1..20); print "替换前 - @nums\n"; splice(@nums, 5, 5, 21..25); print "替换后 - @nums\n"; </sample> 执行以上程序,输出结果为: <pre> 替换前 - 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 替换后 - 1 2 3 4 5 21 22 23 24 25 11 12 13 14 15 16 17 18 19 20 </pre> == 将字符串转换为数组 == Perl 中将字符串转换为数组使用 split() 函数,语法格式如下: <pre> split [ PATTERN [ , EXPR [ , LIMIT ] ] ] </pre> 参数说明: * PATTERN:分隔符,默认为空格。 * EXPR:指定字符串数。 * LIMIT:如果指定该参数,则返回该数组的元素个数。 <sample title="" desc="" lang="perl" hererun="1"> #!/usr/bin/perl # 定义字符串 $var_test = "xiaobai"; $var_string = "www-xiaobai.wang"; $var_names = "google,taobao,xiaobai,weibo"; # 字符串转为数组 @test = split('', $var_test); @string = split('-', $var_string); @names = split(',', $var_names); print "$test[3]\n"; # 输出 o print "$string[2]\n"; # 输出 com print "$names[3]\n"; # 输出 weibo </sample> 执行以上程序,输出结果为: <pre> o com weibo </pre> == 将数组转换为字符串 == Perl 中将数组转换为字符串使用 join() 函数,语法格式如下: <pre> join EXPR, LIST </pre> 参数说明: * EXPR:连接符。 * LIST:列表或数组。 <sample title="" desc="" lang="perl" hererun="1"> #!/usr/bin/perl # 定义字符串 $var_string = "www-xiaobai.wang"; $var_names = "google,taobao,xiaobai,weibo"; # 字符串转为数组 @string = split('-', $var_string); @names = split(',', $var_names); # 数组转为字符串 $string1 = join( '-', @string ); $string2 = join( ',', @names ); print "$string1\n"; print "$string2\n"; </sample> 执行以上程序,输出结果为: <pre> www-xiaobai.wang google,taobao,xiaobai,weibo </pre> == 数组排序 == Perl 中数组排序使用 sort() 函数,语法格式如下: <pre> sort [ SUBROUTINE ] LIST </pre> 参数说明: * SUBROUTINE:指定规则。 * LIST:列表或数组。 <sample title="" desc="" lang="perl" hererun="1"> #!/usr/bin/perl # 定义数组 @sites = qw(google taobao xiaobai facebook); print "排序前: @sites\n"; # 对数组进行排序 @sites = sort(@sites); print "排序后: @sites\n"; </sample> 执行以上程序,输出结果为: <pre> 排序前: google taobao xiaobai facebook 排序后: facebook google xiaobai taobao </pre> <blockquote>注意:数组排序是根据 ASCII 数字值来排序。所以我们在对数组进行排序时最好先将每个元素转换为小写后再排序。 </blockquote> == 特殊变量: $[ == 特殊变量 '''$[''' 表示数组的第一索引值,一般都为 0 ,如果我们将 '''$[''' 设置为 1,则数组的第一个索引值即为 1,第二个为 2,以此类推。实例如下: <sample title="" desc="" lang="perl" hererun="1"> #!/usr/bin/perl # 定义数组 @sites = qw(google taobao xiaobai facebook); print "网站: @sites\n"; # 设置数组的第一个索引为 1 $[ = 1; print "\@sites[1]: $sites[1]\n"; print "\@sites[2]: $sites[2]\n"; </sample> 执行以上程序,输出结果为: <pre> 网站: google taobao xiaobai facebook @sites[1]: google @sites[2]: taobao </pre> <blockquote>一般情况我们不建议使用特殊变量 '''$[''',在新版 Perl 中,该变量已废弃。 </blockquote> == 合并数组 == 数组的元素是以逗号来分割,我们也可以使用逗号来合并数组,如下所示: <sample title="" desc="" lang="perl" hererun="1"> #!/usr/bin/perl @numbers = (1,3,(4,5,6)); print "numbers = @numbers\n"; </sample> 执行以上程序,输出结果为: <pre> numbers = 1 3 4 5 6 </pre> 也可以在数组中嵌入多个数组,并合并到主数组中: <sample title="" desc="" lang="perl" hererun="1"> #!/usr/bin/perl @odd = (1,3,5); @even = (2, 4, 6); @numbers = (@odd, @even); print "numbers = @numbers\n"; </sample> 执行以上程序,输出结果为: <pre> numbers = 1 3 5 2 4 6 </pre> == 从列表中选择元素 == 一个列表可以当作一个数组使用,在列表后指定索引值可以读取指定的元素,如下所示: <sample title="" desc="" lang="perl" hererun="1"> #!/usr/bin/perl $var = (5,4,3,2,1)[4]; print "var 的值为 = $var\n" </sample> 执行以上程序,输出结果为: <pre> var 的值为 = 1 </pre> 同样我们可以在数组中使用 '''..''' 来读取指定范围的元素: <sample title="" desc="" lang="perl" hererun="1"> #!/usr/bin/perl @list = (5,4,3,2,1)[1..3]; print "list 的值 = @list\n"; </sample> 执行以上程序,输出结果为: <pre> list 的值 = 4 3 2 </pre>
返回至“
Perl 数组
”。
上一节:
Perl_标量
下一节:
Perl_哈希