1、从标准输入(STDIN)给变量赋值
$var = ;
@array = ; # ^D为结束输入的符号
2、有关数组的库函数
(1)sort--按字符顺序排序
@array = ("this", "is", "a","test");
@array2 = sort(@array); # @array2 = ("a","is", "test", "this")
@array = (70, 100, 8);
@array = sort(@array); # @array = (100, 70, 8) now
( 2)reverse--反转数组
@array2 = reverse(@array);
@array2 = reverse sort (@array);
(3)chop--数组去尾
chop的意义是去掉STDIN(键盘)输入字符串时最后一个字符--换行符。而如果它作用到数组上,则将数组中每一个元素都做如此处理。
@list = ("rabbit", "12345","quartz");
chop (@list); # @list = ("rabbi", "1234","quart") now
( 4)join/split--连接/拆分
join的第一个参数是连接所用的中间字符,其余则为待连接的字符数组。
$string = join(" ", "this", "is","a", "string"); # 结果为"this is a string"
@list = ("words","and");
$string = join("::", @list, "colons"); #结果为"words::and::colons"
@array = split(/::/,$string); # @array = ("words","and", "colons") now
3、自然数排序,以数字顺序sort
@a=(11,2,3,56);
@a= sort {$a <=> $b} @a;
print "@a\n";
等同于
sub numerically { $a <=>; $b };
print join(' ', sort numerically @array), "\n";
4、打开文件,打开失败输出"could not open file",打开成功后,输出文件内容 ,之后关闭文件
rgopen(tt,"tt")||die("could not open file");
@tt=;
print @tt;
close(tt);
5、循环
6、方法
SHELL中定义方法为function subname() {
}
PERL中定义方法为 sub subname {
}
在方法前调用为&subname ;
可先定义方法后调用 ,则可省略&号;
6、子程序传递参数
sub addlist {
my (@list) = @_;
}
sub sub1 {
my ($num1,$num2,$num3) = @_;
...
}
$var = ;
@array = ; # ^D为结束输入的符号
2、有关数组的库函数
(1)sort--按字符顺序排序
@array = ("this", "is", "a","test");
@array2 = sort(@array); # @array2 = ("a","is", "test", "this")
@array = (70, 100, 8);
@array = sort(@array); # @array = (100, 70, 8) now
( 2)reverse--反转数组
@array2 = reverse(@array);
@array2 = reverse sort (@array);
(3)chop--数组去尾
chop的意义是去掉STDIN(键盘)输入字符串时最后一个字符--换行符。而如果它作用到数组上,则将数组中每一个元素都做如此处理。
@list = ("rabbit", "12345","quartz");
chop (@list); # @list = ("rabbi", "1234","quart") now
( 4)join/split--连接/拆分
join的第一个参数是连接所用的中间字符,其余则为待连接的字符数组。
$string = join(" ", "this", "is","a", "string"); # 结果为"this is a string"
@list = ("words","and");
$string = join("::", @list, "colons"); #结果为"words::and::colons"
@array = split(/::/,$string); # @array = ("words","and", "colons") now
3、自然数排序,以数字顺序sort
@a=(11,2,3,56);
@a= sort {$a <=> $b} @a;
print "@a\n";
等同于
sub numerically { $a <=>; $b };
print join(' ', sort numerically @array), "\n";
4、打开文件,打开失败输出"could not open file",打开成功后,输出文件内容 ,之后关闭文件
rgopen(tt,"tt")||die("could not open file");
@tt=;
print @tt;
close(tt);
5、循环
6、方法
SHELL中定义方法为function subname() {
}
PERL中定义方法为 sub subname {
}
在方法前调用为&subname ;
可先定义方法后调用 ,则可省略&号;
6、子程序传递参数
sub addlist {
my (@list) = @_;
}
sub sub1 {
my ($num1,$num2,$num3) = @_;
...
}
编辑回复