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

Learning Perl 7

发布时间:2020-12-16 00:36:47 所属栏目:大数据 来源:网络整理
导读:?1. 数组的长度 $#arrayname返回数组最后一个长度的下标。( 默认数组是从0下标开始) 2. 数组的切片Array slicing 从一个数组给另一个数组的元素赋值时,得出的数组称为数组片。如果右边的数组大于左边的数组时,未使用的值将放弃。如果小于,多出的左边的值

?1. 数组的长度

$#arrayname返回数组最后一个长度的下标。( 默认数组是从0下标开始)
2. 数组的切片Array slicing
从一个数组给另一个数组的元素赋值时,得出的数组称为数组片。如果右边的数组大于左边的数组时,未使用的值将放弃。如果小于,多出的左边的值将为undef。
例如:
@name = ('A','B','C');
@pal = @name[0,1]; ? ? ? ? ?#important! array slicing,using @name[...] instead of $name[...]
print @pal."@paln";
($pal[2],$pal[3]) = @name[2,1];
print @pal."@paln";
令:
@part = ();
$x = 4;
for ($y = 7; $y < 13; $y++) {
push @part,$AoA[$x][$y];
}
这个循环其实可以用一个切片操作来代替:
?@part = @{ $AoA[4] } [ 7..12 ];
3. 二维数组
第一个下标代表行,第二个代表列。行是未命名的列表,即匿名列表。->运算符,又称中缀运算符可以用来获得数组的单个元素。
@matrix = (
? ? [1,2,3],
? ? [4,5,6],
? ? [],
? ? []
);
?
print $matrix[0][1],$matrix[1]->[1]."n"; ? ?#写法作用相同
$matrix[2][2]=<STDIN>;
print $matrix[2]->[2]."n";
my $i = scalar @{$matrix[0]};
一个指向数组的引用:
?# 一个指向“包含有数组引用的数组”的引用
? ? ? ? $ref_to_AoA = [
? ? ? ? ? ? [ "fred","barney","pebbles","bambam","dino",],
? ? ? ? ? ? [ "homer","bart","marge","maggie",
? ? ? ? ? ? [ "george","jane","elroy","judy",
? ? ? ? ];
? ? ? ? print $ref_to_AoA->[2][2];
与C语言不同,perl中不能随意变换数组和引用。所以访问方式不一样!
让我们试着从一个文件中读取二维数组。首先我们演示如何一次性添加一行。首先我们假设有这样一个文本文件:每一行代表了二维数组的行,而每一个单词代表了二维数组的一个元素。下面的代码可以把它们储存到 @AoA:?
while (<>) {
@tmp = split;
push @AoA,[ @tmp ];
}
当然,你也可以不要临时变量:
while (<>) {push @AoA,[ split ];}
如果你知道想要放在什么地方的话,你也可以不要 push(),而是直接进行赋值:
my (@AoA,$i,$line);
for $i ( 0 .. 10 ) {
$line = <>;
$AoA[$i] = [ split ' ',$line ];
}
你可能生怕 <> 在列表上下文会出差错,所以想要明确地声明要在标量上下文中对 <> 求值,这样可读性会更好一些: (译者注:列表上下文中,<>返回所有的行,标量上下文中 <> 只返回一行。)
my (@AoA,$i);
for $i ( 0 .. 10 ) {
$AoA[$i] = [ split ' ',scalar(<>) ];
? ? ? }
如果你想用 $ref_to_AoA 这样的一个引用来代替数组,那你就得这么写:
? ? ? while (<>) {
? ? ? ? ?push @$ref_to_AoA,[ split ];
}
打印整个数组:不支持对引用的自动展开print运算。需要以此迭代输出:
for $aref ( @AoA ) {
? ? ? ? ? ? print "t [ @$aref ],n";
}
或者
for $i ( 0 .. $#AoA ) {
? ? ? ? ? ? print "t elt $i is [ @{$AoA[$i]} ],n";
}
二重循环输出:
for $i ( 0 .. $#AoA ) {
? ? ? ? ? ? $aref = $AoA[$i];
? ? ? ? ? ? for $j ( 0 .. $#{$aref} ) {
? ? ? ? ? ? ? ? print "elt $i $j is $AoA[$i][$j]n";
? ? ? ? ? ? }
? ? ? ? }
? ? 哦,好像还有点复杂,那么试试这样:
? ? ? ?for $i ( 0 .. $#AoA ) {
? ? ? ? ? ? $aref = $AoA[$i];
? ? ? ? ? ? $n = @$aref - 1;
? ? ? ? ? ? for $j ( 0 .. $n ) {
? ? ? ? ? ? ? ? print "elt $i $j is $AoA[$i][$j]n";
? ? ? ? ? ? }
? ? ? ? }
?
4. 散列的分片
@info = qw(Marine Captain 50000);
@officer{‘BRANCH’,‘TITLE’,‘SALARY’} = @info;
散列的散列
anonymous hashs in a hash
%students=(Math => {Joe => 100,Joan => 95},Science => {Bill => 85},Dan => 76);
获得散列中的匿名散列的元素
$students{Math}->{Joan}
获得整个匿名散列
%{$students{Math}}
anonymous arrays in a hash
%grades=(
Math => [89,90],
English => [79,69]
);
$grades{Math}[1];
$grades{Math}->[1];
?
@{$grades{English}} the anonymous array
散列的数组
@stores=(
{Boss=>”Ari Goldberg”,
Registers=>10
},
{Boss=>”Amy Goldberg”,
Registers=>5
});
the numbers of element in the array is $#array+1
for($i=0; $i<$#sores+1; $i++){
print $stores[$i]->{“Boss”};
print ‘___’ x 20,“n”
}
5. 输入函数
(1). <STDIN>
列表上下文下,每一行将作为列表中的一项。使用EOF结束(UNIX ctrl+D)
@all=<STDIN>
(2).read函数
read函数用于从规定的文件句柄将字节数读到变量中。返回读取到得字节数。如果从标准输入读取,文件句柄为STDIN。
$number = read(STDIN,$favorite,10);
# if the input string less than 10 chars,press ctrl + D to end.
print "$number is $numbern,$favorite is $favoriten";
(3).getc
从键盘或文件获得单个字符,遇到EOF则返回空字符。
$answer=getc;
?
6. 数组函数
(1). exist $ARRAY[index];
print “Hello,$arr[1]” if exist $arr[1];
print “out of range” if not exist $arr[n];
(2).grep函数对于数组LIST的每个元素都需要求出表达式EXPR的值,返回值是标量(表达式为真的个数)或者另一个数组(使表达式为真的元素)。
grep(EXPR,LIST);
@list=(tomatoes,tomorrow,now);
$count=grep(/tom/,list); #count = 2
@rlist=grep(/tom/,list); #rlist = tomatoes,tomorrow
(3).join 与split相反。join(“:”,$hour,$min,$second);
(4).map函数 map EXPR,LIST
@list=(0x64,0x65,0x6e);
@words=map chr,@list; #den
@n=(1,3);
@n=map $_*2+6,@n;
print @n; #8,10,12
(5). map {BLOCK} LIST
块格式,一般配合split函数。生成列表,使得字段成为数组的元素。
open(PSWD,“/etc/passwd”);
@lines=<PSWD>;
@fields=map {split(“:”)} @lines
foreach $field (@fields) {
print $field.”n”;
}
?
参考
《Perl by example》
?Perl 二维数组 ? ? http://bbs.chinaunix.net/viewthread.php?tid=770848

(编辑:李大同)

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

    推荐文章
      热点阅读