初始化
@title = ("the", "black", "cat");
@title = qw(the black cat);
@letters = ('A'..'Z')
@numbers = (1..9)
增加、删除
$last = pop (@title); //删除最右侧
$first = shift(@title); //删除最左侧
push (@title, "any"); //添加最右侧
unshift (@title, "any"); //添加最左侧
循环 / 旋转
unshift (@title,pop(@title)) //尾部移到头部
push (@title, shift(@title) //头部移到尾部
打印
print @title; //没有空格
print “@title” //自动空格
匹配
$regex = qr/(/w)/1/;
@subset = grep (/$regex/, @numbers); //生成子数组
排序
@sorted = sort (@words); //字符顺序
@sorted = sort { $a cmp $b } @words //字符顺序
@sorted = sort { $b cmp $a } @words //字符逆序
@sorted = reverse sort { $a cmp $b } @words //字符逆序
@sorted = sort { $a <=> $b }; //数值顺序
@sorted = sort { $b <=> $a }; //数值逆序
自定义排序
@sorted = sort byLength @words;
sub byLength {
$value = (length($a) <=> length($b))
if ($value == 0) {
return lc($a) cmp lc($b);
} else {
return $value;
}
}
Perl using Arrays
最新推荐文章于 2024-08-08 13:28:00 发布