【Perl读书笔记】基本数组,初始化

这篇博客摘录自《C程序员精通Perl》的3.1节,通过实例展示了Perl中数组的基本操作,包括初始化和元素访问。文章通过`array.pl`和`array1.pl`两个脚本演示了不同方式的数组初始化,并输出了数组的长度、元素值以及找到的最小值和最大值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

读 《C程序员精通Perl》http://book.douban.com/subject/1232075/   3.1节 笔记



#!/usr/bin/perl

use strict;
use warnings;

my @array = (10, 20, 30, 3, 38, 7, 80);
my $biggest;
my $smallest;

$biggest = $array[0];
$smallest = $array[0];

print "number of array = $#array\n";

for (my $index=1; $index <= $#array; $index++) {

        print "array[$index] = $array[$index]\n";

        if ($array[$index] > $biggest) {

                $biggest = $array[$index];
        }   


        if ($array[$index] < $smallest) {

                $smallest = $array[$index];
        }   
}

print "smallest = $smallest, biggest = $biggest\n";

运行结果:

[root@localhost perl_practice]# ./array.pl
number of array = 6
array[1] = 20
array[2] = 30
array[3] = 3
array[4] = 38
array[5] = 7
array[6] = 80
smallest = 3, biggest = 80
[root@localhost perl_practice]#


初始化方式1:

#!/usr/bin/perl

use strict;
use warnings;

my $biggest = 2;
my $smallest = 100;
my @array_sub = ($biggest, $smallest);
my @array = ($biggest, $smallest, @array_sub);


print "number of array = $#array\n";

for (my $index=0; $index <= $#array; $index++) {

        print "array[$index] = $array[$index]\n";

}

print "array_sub are: @array_sub\n";
print "array are: @array\n";

运行结果:

[root@localhost perl_practice]# ./array1.pl   

number of array = 3

array[0] = 2

array[1] = 100

array[2] = 2

array[3] = 100

array_sub are: 2 100

array are: 2 100 2 100

[root@localhost perl_practice]#


初始化2:

#!/usr/bin/perl

use strict;
use warnings;

my @array = (1);

$array[5] = "hello";

print "number of array = $#array\n";

for (my $index=0; $index <= $#array; $index++) {

        print "array[$index] = $array[$index]\n";

}

#print "array are: @array\n";


运行结果:

[root@localhost perl_practice]# ./array2.pl
number of array = 5
array[0] = 1
Use of uninitialized value in concatenation (.) or string at ./array2.pl line 14.
array[1] =
Use of uninitialized value in concatenation (.) or string at ./array2.pl line 14.
array[2] =
Use of uninitialized value in concatenation (.) or string at ./array2.pl line 14.
array[3] =
Use of uninitialized value in concatenation (.) or string at ./array2.pl line 14.
array[4] =
array[5] = hello
[root@localhost perl_practice]#

初始化3:

#!/usr/bin/perl

use strict;
use warnings;

my @array = qw(hello calf_man 99);


print "number of array = $#array\n";

for (my $index=0; $index <= $#array; $index++) {

        print "array[$index] = $array[$index]\n";

}

print "array are: @array\n";

运行结果:

[root@localhost perl_practice]# ./array3.pl
number of array = 2
array[0] = hello
array[1] = calf_man
array[2] = 99
array are: hello calf_man 99
[root@localhost perl_practice]#











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值